Przejdź do głównej zawartości

std::multiset extract() method

// (1) Non const version only
node_type extract( const_iterator position );

// (2) Non const version only
node_type extract( const Key& k );

// (3) Non const version only
template< class K >
node_type extract( K&& x );
  • (1) Unlinks the node that contains the element pointed to by position and returns a node handle that owns it.
  • (2) If the container has an element with key equivalent to k, unlinks the node that contains that element from the container and returns a node handle that owns it. Otherwise, returns an empty node handle.
  • (3) Same as (2). This overload participates in overload resolution only if the qualified-id Compare::is_transparent is valid and denotes a type, and neither iterator nor const_iterator is implicitly convertible from K. It allows calling this function without constructing an instance of Key.

In either case, no elements are copied or moved, only the internal pointers of the container nodes are repointed (rebalancing may occur, as with erase()).

Invalidation

Extracting a node invalidates only the iterators to the extracted element.

important

Pointers and references to the extracted element remain valid, but cannot be used while element is owned by a node handle: they become usable if the element is inserted into a container.

Parameters

  • position - a valid iterator into this container
  • k - a key to identify the node to be extracted
  • x - a value of any type that can be transparently compared with a key identifying the node to be extracted

Return value

A node handle that owns the extracted element, or empty node handle in case the element is not found (overloads (2) and (3)).

Exceptions

  • (1) Throws nothing.
  • (2-3) Any exceptions thrown by the Compare object.

Complexity

  • (1) Amortized constant - O(1).
  • (2-3) Linear in the size of the container - O(log size()).

Notes

Using extract() is the only way to change a key of a multiset element without reallocation:

std::set<move_only_type> s;
s.emplace(...);
move_only_type mot = std::move(s.extract(s.begin()).value());

Feature testing macro: __cpp_lib_associative_heterogeneous_erasure (for overload (3)).

Example

Main.cpp
#include <algorithm>
#include <iostream>
#include <string_view>
#include <set>

void print(std::string_view comment, const auto& data)
{
std::cout << comment;
for (auto datum : data)
std::cout << ' ' << datum;

std::cout << '\n';
}

int main()
{
std::multiset<int> cont{1, 2, 3};

print("Start:", cont);

// Extract node handle and change key
auto nh = cont.extract(1);
nh.value() = 4;

print("After extract and before insert:", cont);

// Insert node handle back
cont.insert(std::move(nh));

print("End:", cont);
}
Output
Start: 1 2 3
After extract and before insert: 2 3
End: 2 3 4
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::multiset extract() method

// (1) Non const version only
node_type extract( const_iterator position );

// (2) Non const version only
node_type extract( const Key& k );

// (3) Non const version only
template< class K >
node_type extract( K&& x );
  • (1) Unlinks the node that contains the element pointed to by position and returns a node handle that owns it.
  • (2) If the container has an element with key equivalent to k, unlinks the node that contains that element from the container and returns a node handle that owns it. Otherwise, returns an empty node handle.
  • (3) Same as (2). This overload participates in overload resolution only if the qualified-id Compare::is_transparent is valid and denotes a type, and neither iterator nor const_iterator is implicitly convertible from K. It allows calling this function without constructing an instance of Key.

In either case, no elements are copied or moved, only the internal pointers of the container nodes are repointed (rebalancing may occur, as with erase()).

Invalidation

Extracting a node invalidates only the iterators to the extracted element.

important

Pointers and references to the extracted element remain valid, but cannot be used while element is owned by a node handle: they become usable if the element is inserted into a container.

Parameters

  • position - a valid iterator into this container
  • k - a key to identify the node to be extracted
  • x - a value of any type that can be transparently compared with a key identifying the node to be extracted

Return value

A node handle that owns the extracted element, or empty node handle in case the element is not found (overloads (2) and (3)).

Exceptions

  • (1) Throws nothing.
  • (2-3) Any exceptions thrown by the Compare object.

Complexity

  • (1) Amortized constant - O(1).
  • (2-3) Linear in the size of the container - O(log size()).

Notes

Using extract() is the only way to change a key of a multiset element without reallocation:

std::set<move_only_type> s;
s.emplace(...);
move_only_type mot = std::move(s.extract(s.begin()).value());

Feature testing macro: __cpp_lib_associative_heterogeneous_erasure (for overload (3)).

Example

Main.cpp
#include <algorithm>
#include <iostream>
#include <string_view>
#include <set>

void print(std::string_view comment, const auto& data)
{
std::cout << comment;
for (auto datum : data)
std::cout << ' ' << datum;

std::cout << '\n';
}

int main()
{
std::multiset<int> cont{1, 2, 3};

print("Start:", cont);

// Extract node handle and change key
auto nh = cont.extract(1);
nh.value() = 4;

print("After extract and before insert:", cont);

// Insert node handle back
cont.insert(std::move(nh));

print("End:", cont);
}
Output
Start: 1 2 3
After extract and before insert: 2 3
End: 2 3 4
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.