I\'m accustomed to the existing interface of std::map
.
Inserting elements returns a bool describing successful insertion,
as well the iterator as to where t
You can obtain a node from a std::map
using its extract() function. Then you can put that node into another map (or into the same map after changing its key).
node_type extract(const_iterator position); (1) (since C++17) node_type extract(const key_type& x); (2) (since C++17)
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 tox
, unlinks the node that contains that element from the container and returns a node handle that owns it. Otherwise, returns an empty node handle.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 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.
Example:
map
m{{1,”mango”}, {2,”papaya”}, {3,”guava”}}; auto nh = m.extract(2); nh.key() = 4; m.insert(move(nh)); // m == {{1,”mango”}, {3,”guava”}, {4,”papaya”}}