如何修改std :: map容器中的键值 [英] How to modify key values in std::map container

查看:1074
本文介绍了如何修改std :: map容器中的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定

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?

推荐答案

看起来你是最好的建立一个新的地图,并在之后进行交换。您只有 n 个插入操作,而不是 n 删除和 n 插入。

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.

这篇关于如何修改std :: map容器中的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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