简体   繁体   English

如何从 std::map 修改提取的节点密钥

[英]How to modify an extracted node key from an std::map

In a previous question , I wanted to store some extracted nodes from an std::map into a std::vector<std::map<K, V>::node_type> , using a for loop.上一个问题中,我想使用for循环将一些从std::map提取的节点存储到std::vector<std::map<K, V>::node_type>中。 Now that I can do that, I'd like to modify the nodes keys before storing them in the vector , and I'm getting a compilation error (not copying the whole error message, it's very long):现在我可以做到这一点,我想在将节点键存储到vector之前修改它们,并且我得到一个编译错误(没有复制整个错误消息,它很长):

/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/bits/alloc_traits.h:514:4: error: no matching function for call to 'construct_at'
          std::construct_at(__p, std::forward<_Args>(__args)...);
          ^~~~~~~~~~~~~~~~~

Here is what I'm doing:这是我正在做的事情:

  std::vector<decltype(myMap)::node_type> tmp;
  for (auto it = myMap.begin(); it != myMap.end();) {
    if (it->second->needsToBeModified()) {
      auto out = it++;
      auto node = myMap.extract(out);
      node.key() = someNewKey;
      tmp.push_back(node); // The above error message points to this line
    } else
      it++;
  }

If I don't put the node into a variable, the error is gone:如果我不将节点放入变量中,错误就消失了:

  std::vector<decltype(myMap)::node_type> tmp;
  for (auto it = myMap.begin(); it != myMap.end();) {
    if (it->second->needsToBeModified()) {
      auto out = it++;
      tmp.push_back(myMap.extract(out)); // This is fine
    } else
      it++;
  }

So I guess there is something with the way I manipulate the node but I can't figure it out yet.所以我想我操纵节点的方式有些问题,但我还不能弄清楚。 Could it be possible the problem is with my key which is an std::pair<int16_t, uint32_t> ?问题可能出在我的密钥std::pair<int16_t, uint32_t>吗?

Node handles are not copyable.节点句柄不可复制。 Thus, you cannot create a copy in the vector.因此,您无法在矢量中创建副本。

Node handles are movable though, which is why the other example works.节点句柄是可移动的,这就是其他示例有效的原因。

Moving from a variable can be done using the standard function std::move :可以使用标准 function std::move从变量中移动:

tmp.push_back(std::move(node))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM