- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ multiset Library - extract() Function
Description
The multiset::extract() function in C++ STL was introduced in C++17 that extracts a node from the multiset container. It removes the node from the multiset and returns a node handle that contains the extracted element. After extraction, the extracted node is removed from the multiset and does not exist in the original multiset.
Syntax
Following is the syntax of multiset::extract() function −
// Extract by iterator node_type extract(const_iterator position); // Extract by key node_type extract(const key_type& key); // Extract by key-like object template< class K > node_type extract( K&& keyObj );
Parameters
The extract() function accepts a single parameter. It can be an iterator that points to the element to be extracted or the key of the element we want to extract.
Return value
The extract() function returns a node handle that contains extracted element. If the key is not found, it returns an empty node handle.
Exceptions
If an exception is thrown, the container remains unchanged.
Time complexity
The time complexity of multiset::extract() function is O(1) using iterator, and O(log N) using key and key-like object, where N is the size of the multiset.
Examples of multiset::extract() Function
The following examples demonstrate the usage of multiset::extract() function in multiset −
Extracting an Element by Iterator
Below is an example to extract an element (30) from a multiset nums using an iterator it with the multiset::extract() function −
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> nums = {10, 20, 30, 40, 50};
cout << "Multiset before extraction:";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
auto it = nums.find(30);
auto node = nums.extract(it);
cout << "Extracted element: " << node.value() << endl;
cout << "Multiset after extraction:";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
return 0;
}
The output of the above code is given below −
Multiset before extraction: 10 20 30 40 50 Extracted element: 30 Multiset after extraction: 10 20 40 50
Extracting an Element by Key
In this example, we have used the multiset::extract() function to extract an element using its key(20) from the multiset nums.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> nums = {10, 20, 20, 30, 40};
cout << "Multiset before extraction:";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
auto node = nums.extract(20);
if (!node.empty()) {
cout << "Extracted element: " << node.value() << endl;
}
cout << "Multiset after extraction:";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
return 0;
}
The output of the above code is given below −
Multiset before extraction: 10 20 20 30 40 Extracted element: 20 Multiset after extraction: 10 20 30 40
Moving Extracted Node to Another Multiset
Here is an example to extract and move an element from one multiset mset1 to another multiset mset2 using the multiset::extract() function −
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> mset1 = {10, 20, 30, 40};
multiset<int> mset2 = {50, 60, 70};
cout << "Multiset 1 before extraction:";
for (const auto &num : mset1)
cout << " " << num;
cout << endl;
cout << "Multiset 2 before insertion:";
for (const auto &num : mset2)
cout << " " << num;
cout << endl;
auto node = mset1.extract(30);
mset2.insert(move(node));
cout << "\nMultiset 1 after extraction:";
for (const auto &num : mset1)
cout << " " << num;
cout << endl;
cout << "Multiset 2 after insertion:";
for (const auto &num : mset2)
cout << " " << num;
cout << endl;
return 0;
}
The output of the above code is given below −
Multiset 1 before extraction: 10 20 30 40 Multiset 2 before insertion: 50 60 70 Multiset 1 after extraction: 10 20 40 Multiset 2 after insertion: 30 50 60 70