我什么时候使用 node_type 和 std::map::insert?
When do I use node_type with std::map::insert?
我已经习惯了std::map
的现有界面。
插入元素 returns 描述插入成功的布尔值,
以及关于插入元素所在位置的迭代器。
template< class P >
std::pair<iterator,bool> insert( P&& value ); //(since C++11)
C++17 添加了类似的调用,但类型名称不同:
insert_return_type insert(node_type&& nh); //(since C++17)
我试着查找 node_type
是什么,但它主要是未指定的:
template</*unspecified*/>
class /*unspecified*/
为什么在 C++17 中添加了这个函数,我什么时候可以在旧调用中使用它?
您可以使用 extract() 函数从 std::map
获取节点。然后你可以将该节点放入另一个地图(或更改其键后放入同一个地图)。
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 to x
, 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<int, string> 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”}}
不仅仅是std::map
,所有关联和无序的关联容器都添加了类似的功能。它们在 [container.node]/1
中的标准中进行了解释
A node handle is an object that accepts ownership of a single element from an associative container (23.2.4) or an unordered associative container (23.2.5). It may be used to transfer that ownership to another container
with compatible nodes. Containers with compatible nodes have the same node handle type. ...
上一节后面的 table 显示了具有兼容节点的容器。
节点句柄接口允许您将元素(节点)从一个容器传输到另一个(兼容)容器,而无需 copy/move 元素。而是将容器维护的各个内部节点作为一个整体进行传输。
在处理包含不可复制、不可移动类型的容器时,这变得很有必要。
我已经习惯了std::map
的现有界面。
插入元素 returns 描述插入成功的布尔值,
以及关于插入元素所在位置的迭代器。
template< class P >
std::pair<iterator,bool> insert( P&& value ); //(since C++11)
C++17 添加了类似的调用,但类型名称不同:
insert_return_type insert(node_type&& nh); //(since C++17)
我试着查找 node_type
是什么,但它主要是未指定的:
template</*unspecified*/>
class /*unspecified*/
为什么在 C++17 中添加了这个函数,我什么时候可以在旧调用中使用它?
您可以使用 extract() 函数从 std::map
获取节点。然后你可以将该节点放入另一个地图(或更改其键后放入同一个地图)。
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<int, string> 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”}}
不仅仅是std::map
,所有关联和无序的关联容器都添加了类似的功能。它们在 [container.node]/1
A node handle is an object that accepts ownership of a single element from an associative container (23.2.4) or an unordered associative container (23.2.5). It may be used to transfer that ownership to another container with compatible nodes. Containers with compatible nodes have the same node handle type. ...
上一节后面的 table 显示了具有兼容节点的容器。
节点句柄接口允许您将元素(节点)从一个容器传输到另一个(兼容)容器,而无需 copy/move 元素。而是将容器维护的各个内部节点作为一个整体进行传输。
在处理包含不可复制、不可移动类型的容器时,这变得很有必要。