A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../../cpp/language/../container/map/extract.html below:

std::map<Key,T,Compare,Allocator>::extract - cppreference.com

node_type extract( const_iterator pos );

(1) (since C++17)
(constexpr since C++26)

node_type extract( const Key& k );

(2) (since C++17)
(constexpr since C++26)

template< class K >
node_type extract( K&& x );

(3) (since C++23)
(constexpr since C++26) 1)

Unlinks the node that contains the element pointed to by

pos

and returns a

node handle

that owns it.

2,3)

If the container has an element with key equivalent to

k or x(since C++23)

, 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)

This overload participates in overload resolution only if

Compare

is

transparent

, 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()).

Extracting a node invalidates only the iterators to the extracted element. 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.

[edit] Parameters pos - 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 [edit] Return value

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

[edit] Exceptions

1) Throws nothing.

2,3) Any exceptions thrown by the Compare object.

[edit] Complexity

1) Amortized constant.

[edit] Notes

extract is the only way to change a key of a map element without reallocation:

std::map<int, std::string> m{{1, "mango"}, {2, "papaya"}, {3, "guava"}};
auto nh = m.extract(2);
nh.key() = 4;
m.insert(std::move(nh));
// m == {{1, "mango"}, {3, "guava"}, {4, "papaya"}}
[edit] Example
#include <algorithm>
#include <iostream>
#include <string_view>
#include <map>
 
void print(std::string_view comment, const auto& data)
{
    std::cout << comment;
    for (auto [k, v] : data)
        std::cout << ' ' << k << '(' << v << ')';
 
    std::cout << '\n';
}
 
int main()
{
    std::map<int, char> cont{{1, 'a'}, {2, 'b'}, {3, 'c'}};
 
    print("Start:", cont);
 
    // Extract node handle and change key
    auto nh = cont.extract(1);
    nh.key() = 4;
 
    print("After extract and before insert:", cont);
 
    // Insert node handle back
    cont.insert(std::move(nh));
 
    print("End:", cont);
}

Output:

Start: 1(a) 2(b) 3(c)
After extract and before insert: 2(b) 3(c)
End: 2(b) 3(c) 4(a)
[edit] See also splices nodes from another container
(public member function) [edit] inserts elements or nodes(since C++17)
(public member function) [edit] erases elements
(public member function) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4