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
multiset.htm
Advertisements