如何使用提取和插入更改std :: map中的某些键 [英] How to change some keys in a std::map using extract and insert

查看:49
本文介绍了如何使用提取和插入更改std :: map中的某些键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了关于node_type的这个问题,并且我了解到,如果需要修改地图中特定节点的键,则可以提取该节点,修改键,然后 insert 该节点返回到地图(例如此示例).

I've recently read this question about node_type, and I learned that if I need to modify the key of a specific node in a map, I could extract the node, modify the key and insert the node back in the map (like in this example).

现在,假设我需要更改此映射的多个节点的键,并且要知道需要修改哪些节点,必须迭代这些值.我认为我不应该在迭代时将节点重新插入地图中(但也许我错了,请参阅问题的结尾),所以我想我可以在第一次迭代地图时,提取节点当我完成对地图的迭代和修改之后,需要修改,将其存储在向量中,然后再次插入节点.

Now, let's say I need to change the key of multiple nodes of this map, and to know which nodes I need to modify, I have to iterate over the values. I think I shouldn't insert the nodes back in the map while iterating (but maybe I'm wrong, see the end of the question), so I was thinking I could iterate over the map in a first time, extract the nodes I need to modify, store those in a vector, and insert the nodes back in a second time, when I'm finished iterating the map and modifying the nodes.

我怀疑我可以通过将密钥存储在向量中而不是节点中来实现这一点,但是由于关键在于更改密钥,因此我认为使用似乎是为此目的而设计的节点可能会很方便

I suspect I could achieve that by storing the keys in a vector, not the nodes, but since the whole point is to change the keys, I thought it could be handy to use the nodes which seem to be made for that purpose.

所以,基本上,这就是我到目前为止所拥有的:

So, basically, here's what I have so far:

  std::vector</*don't know what to put here since node_type is undefined*/> tmp;
  for (auto& it : myMap) {
    if(it.second->needsToBeModified()){
      tmp.push_back(myMap.extract(it.first));
    }
  }

我不知道tmp向量是什么类型.

I can't figure out what type that tmp vector would be.

正如我之前所说,我不想在迭代时插回节点可能是错误的,请阅读答案声明在迭代时在地图中插入或删除不会使迭代器无效(除已删除的条目外).但这感觉很奇怪,如果在迭代时在映射中插入一个节点会发生什么,插入的节点会出现在迭代循环中吗?会导致无限循环吗?

As I said before, I may be wrong about not wanting to insert back the nodes while iterating, reading this answer stating that inserting or deleting in a map while iterating doesn't invalidate iterators (apart from the deleted entry). But this feels weird, what would happen if I insert a node in the map while iterating over it, would the inserted node come up in the iterating loop? Could that lead in an infinite loop?

推荐答案

向量类型很容易解决,因为 std:map 专门化技术公开了类型别名.

The vector type is simple to address, since std:map specializations expose a type alias.

std::vector<std::map<K,V>::node_type> tmp;

涉及更多的点是迭代.您不能将简单的基于范围用于.那是因为节点提取会使迭代到提取元素的迭代器无效.包括在后台进行迭代的迭代器.取而代之的是更合适的循环

The more involved point is the iteration. You cannot use a simple range-based for. That is because node extraction invalidates iterators to the extracted element. And that includes the iterator used behind the scenes for iteration. Instead, a more appropriate loop would be

for (auto it = myMap.begin() ; it != myMap.end();) {
    if(it->second->needsToBeModified()){
      auto out = it++;
      tmp.push_back(myMap.extract(out));
    }
    else
      ++it;
}

可以在迭代时插入(因为它不会使迭代器无效).但是,如果迭代顺序很重要(即使仅出于可调试性考虑),我认为像您使用的辅助向量也是合适的.

Insertion while iterating is possible (since it doesn't invalidate iterators). But if the iteration order is important (even if only for debuggability), I think an auxiliary vector like you use is appropriate.

这篇关于如何使用提取和插入更改std :: map中的某些键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆