如何更新map、multimap中得key
来源:互联网 发布:天天特价淘宝网 编辑:程序博客网 时间:2024/06/08 05:26
首先看一下在stl中的语法:template <typename K, typename V, typename Compare = less<K>, typename Allocator = allocator<pair<const K, V> > >class map; template <typename K, typename V, typename Compare = less<K>, typename Allocator = allocator<pair<const K, V> > >class multimap;
map和multimap的定义差不多,除了multimap可以有多个相同的key值, map根据Compare来对key进行排序,Compaer可以是less,greater等stl自带的,你也可以自己定义Compare。
现在回到正题,如何改变key值,其实没有什么办法,只有选择插入删除操作:即插入新的pair对,删除旧的pair对。 详见下面的code:
namespace My_Utility{template <typename CONTAINER> void replace_key(CONTAINER& container, const typename CONTAINER::key_type& oldKey, const typename CONTAINER::key_type& newKey) { if(!container.key_comp()(oldKey,newKey) && !container.key_comp()(newKey,oldKey)){ return;} //Thanks to Graham for this Fix typename CONTAINER::iterator begin(container.find(oldKey)); for(;;){ if(begin != container.end()){ container.insert(typename CONTAINER::value_type(newKey, begin->second)); container.erase(begin); // Thanks to Graham for this potential fix begin = container.find(oldKey); } else{ return; } } }}
原文链接: http://www.codeproject.com/Articles/36326/How-To-Update-Your-Const-Key-Fields-in-a-Map-Multi
- 如何更新map、multimap中得key
- Muiltset和MultiMap学习--如何让map存放相同的key
- STL中map,multimap,hashmap的区别。
- STL中map,unordered_map,multimap的区别
- 如何取出 Map中key和value的值
- 如何取出 Map中key和value的值
- 如何取出 Map中key和value的值
- 如何取出 Map中key和value的值
- 如何取出 Map中key和value的值1
- 如何取出 Map中key和value的值
- 如何取出集合Map中key和value的值
- Map中如何实现key唯一不重复
- multimap获取key列表
- 如何android google map key
- map/multimap中的key值因常量性不能被直接修改
- STL中HashTable HashMap HashSet Set Map MultiSet MultiMap总结
- 数据结构::关于STL中map,set,muliset,multimap要说的
- Map中key值问题
- session错误
- 各种排序算法的稳定性和时间复杂度小结
- 开发利器 subline 插件安装
- nginx
- edittext 设置显示长度(像素),字符长度 添加省略号
- 如何更新map、multimap中得key
- config group quota for smb shares in Ubuntu
- Adobe Flash Player 下载
- jQuery的对html元素及css的简单操作demo
- Linux 自动上传备份文件
- 如何解决SqlServer2008修改表结构后无法保存的问题
- JInternalFrame标题栏设置
- Spring配置文件中使用PropertyPlaceholderConfigurer配置获取jdbc的配置信息
- Android.animation cts fail之消息处理延时