如何修改std::map容器中的键值

时间:2021-06-12 16:47:26

Given

鉴于

std::map<int,std::string> myMap;
fillMyMapWithStuff(myMap);

// modify key values - I need to add a constant value to each key
for (std::map<int,std::string>::iterator mi=myMap.begin(); mi != myMap.end(); ++mi)
{
    // ...
}

Whats a good way apply some re-indexing? Must I remove the old entry and add a new one with the new key and old value?

什么是一个好的方法应用一些重新索引?我必须删除旧条目并添加一个新键和旧值的新条目吗?

5 个解决方案

#1


8  

Looks like you are better off building a new map and swapping it afterward. You'll have only n insert operations instead of n deletions and n insertions.

看起来你最好先建一个新的地图,然后再交换。你只有n个插入操作而不是n个删除和n个插入。

#2


5  

Yes, you have to remove the old entry and add a new one with the new key. Keys are not modifiable.

是的,您必须删除旧条目,并使用新键添加一个新的条目。键是不可修改的。

If you were modifying only one or a few elements, you could do it efficiently by hinting map::insert with the position of the new element. Since your new keys are sure to be located somewhere after the old keys, you can hint with the iterator that points at the old element. However, you'd have to take care not to re-evaluate the freshly-inserted keys (by iterating end to front for example), and in case of modifying the entire map, it's more efficient to just build a new one.

如果您只修改了一个或几个元素,您可以通过hinting map::insert with the position of the new element来有效地进行修改。由于您的新键肯定位于旧键之后的某个位置,您可以使用指向旧元素的迭代器进行提示。但是,您必须注意不要重新评估新插入的键(例如,从端到前端进行迭代),并且在修改整个映射时,只构建一个新的键会更有效。

#3


3  

Yes, you must. The key is const while it is in the map.

是的,你必须。关键是在地图上的时候。

#4


2  

I think you'll have to construct a new map. If you delete and add new keys within the loop, it might destroy the integrity of iterating over the set of old keys, and not touching the just-inserted keys. (Unless you know how your keys are distributed and put your own logic in there.)

我想你得画一幅新的地图。如果在循环中删除并添加新键,可能会破坏遍历旧键集的完整性,并且不会触及到刚刚插入的键。(除非您知道密钥是如何分布的,并将自己的逻辑放在其中。)

std::map<int,std::string> myMap;
fillMyMapWithStuff(myMap);

std::map<int,std::string> newMap;

// modify key values - I need to add a constant value to each key
for (std::map<int,std::string>::iterator mi=myMap.begin(); mi != myMap.end(); ++mi)
{
    newMap[mi->first] = mi->second;
}

#5


2  

There's one more option. If this operation is a significant feature of your collection, and performance is important, you can avoid copying the map altogether. You can create a class overloading operator[], as well as other accessors and mutators, and add the current shift of the key value.

还有一个选择。如果这个操作是您的集合的一个重要特性,并且性能很重要,那么您可以避免完全复制该映射。您可以创建一个类重载操作符[],以及其他访问器和修改器,并添加键值的当前移位。

#1


8  

Looks like you are better off building a new map and swapping it afterward. You'll have only n insert operations instead of n deletions and n insertions.

看起来你最好先建一个新的地图,然后再交换。你只有n个插入操作而不是n个删除和n个插入。

#2


5  

Yes, you have to remove the old entry and add a new one with the new key. Keys are not modifiable.

是的,您必须删除旧条目,并使用新键添加一个新的条目。键是不可修改的。

If you were modifying only one or a few elements, you could do it efficiently by hinting map::insert with the position of the new element. Since your new keys are sure to be located somewhere after the old keys, you can hint with the iterator that points at the old element. However, you'd have to take care not to re-evaluate the freshly-inserted keys (by iterating end to front for example), and in case of modifying the entire map, it's more efficient to just build a new one.

如果您只修改了一个或几个元素,您可以通过hinting map::insert with the position of the new element来有效地进行修改。由于您的新键肯定位于旧键之后的某个位置,您可以使用指向旧元素的迭代器进行提示。但是,您必须注意不要重新评估新插入的键(例如,从端到前端进行迭代),并且在修改整个映射时,只构建一个新的键会更有效。

#3


3  

Yes, you must. The key is const while it is in the map.

是的,你必须。关键是在地图上的时候。

#4


2  

I think you'll have to construct a new map. If you delete and add new keys within the loop, it might destroy the integrity of iterating over the set of old keys, and not touching the just-inserted keys. (Unless you know how your keys are distributed and put your own logic in there.)

我想你得画一幅新的地图。如果在循环中删除并添加新键,可能会破坏遍历旧键集的完整性,并且不会触及到刚刚插入的键。(除非您知道密钥是如何分布的,并将自己的逻辑放在其中。)

std::map<int,std::string> myMap;
fillMyMapWithStuff(myMap);

std::map<int,std::string> newMap;

// modify key values - I need to add a constant value to each key
for (std::map<int,std::string>::iterator mi=myMap.begin(); mi != myMap.end(); ++mi)
{
    newMap[mi->first] = mi->second;
}

#5


2  

There's one more option. If this operation is a significant feature of your collection, and performance is important, you can avoid copying the map altogether. You can create a class overloading operator[], as well as other accessors and mutators, and add the current shift of the key value.

还有一个选择。如果这个操作是您的集合的一个重要特性,并且性能很重要,那么您可以避免完全复制该映射。您可以创建一个类重载操作符[],以及其他访问器和修改器,并添加键值的当前移位。