免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 35818 | 回复: 8

[C++] c++ map 相同key不会覆盖? [复制链接]

论坛徽章:
0
发表于 2014-02-28 22:12 |显示全部楼层
#include <map>
#include <string>
#include <iostream>
using namespace std;

map<int, string> mapStudent;
int main() {
    mapStudent.insert(pair<int, string>(3, "student_one"));
    mapStudent.insert(pair<int, string>(3, "student_tw"));
    mapStudent.insert(pair<int, string>(3, "student_three"));
    map<int, string>::iterator  iter;
    iter = mapStudent.find(3);
    printf("%s\n", iter->second.c_str());
    return 0;
}

执行结果是:student_one

为什么后续插入的没有覆盖最初插入的呢?
我听说是会覆盖的

刚接触C++
请大家指教一下了

论坛徽章:
5
狮子座
日期:2013-08-20 10:12:24午马
日期:2013-11-23 18:04:102015年辞旧岁徽章
日期:2015-03-03 16:54:152015亚冠之德黑兰石油
日期:2015-06-29 18:11:1115-16赛季CBA联赛之新疆
日期:2024-02-21 10:00:53
发表于 2014-02-28 22:23 |显示全部楼层
Because element keys in a map are unique, the insertion operation checks whether each inserted element has a key equivalent to the one of an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).


十个字补丁……

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2014-02-28 22:45 |显示全部楼层
map -->> multimap

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
发表于 2014-03-01 10:15 |显示全部楼层
回复 1# tianhailong

insert时相同的key不会被覆盖,你想表达的应该是下标访问表达式,map[key] =value;这样的调用是会被重写相应key的值的。
   

论坛徽章:
1
巨蟹座
日期:2014-03-18 23:44:30
发表于 2014-03-03 13:07 |显示全部楼层
Because element keys in a map are unique, the insertion operation checks whether each inserted element has a key equivalent to the one of an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).
楼主不看二楼的回复!

论坛徽章:
0
发表于 2014-03-03 19:13 |显示全部楼层
当遇到相同的键值时无法插入,所以也就无法覆盖了

论坛徽章:
4
双子座
日期:2014-08-28 10:08:002015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:112015年亚洲杯之阿联酋
日期:2015-03-13 03:25:15
发表于 2014-03-03 19:25 |显示全部楼层
map的insert是有返回值的,insert重复的会有错误,这个技巧在面试如何找出2个数组相同的数字的时候有奇效

论坛徽章:
89
水瓶座
日期:2014-04-01 08:53:31天蝎座
日期:2014-04-01 08:53:53天秤座
日期:2014-04-01 08:54:02射手座
日期:2014-04-01 08:54:15子鼠
日期:2014-04-01 08:55:35辰龙
日期:2014-04-01 08:56:36未羊
日期:2014-04-01 08:56:27戌狗
日期:2014-04-01 08:56:13亥猪
日期:2014-04-01 08:56:02亥猪
日期:2014-04-08 08:38:58程序设计版块每日发帖之星
日期:2016-01-05 06:20:00程序设计版块每日发帖之星
日期:2016-01-07 06:20:00
发表于 2014-03-03 21:00 |显示全部楼层
回复 7# weishuo1999

应该是排序之后遍历更快吧。


   

论坛徽章:
1
双子座
日期:2014-04-20 13:05:34
发表于 2014-03-03 21:46 |显示全部楼层
C++ STL 的实现:
1.vector  底层数据结构为数组 ,支持快速随机访问
2.list    底层数据结构为双向链表,支持快速增删
3.deque   底层数据结构为一个中央控制器和多个缓冲区,详细见STL源码剖析P146,支持首尾(中间不能)快速增删,也支持随机访问
4.stack   底层一般用23实现,封闭头部即可,不用vector的原因应该是容量大小有限制,扩容耗时
5.queue   底层一般用23实现,封闭头部即可,不用vector的原因应该是容量大小有限制,扩容耗时
6.45是适配器,而不叫容器,因为是对容器的再封装
7.priority_queue 的底层数据结构一般为vector为底层容器,堆heap为处理规则来管理底层容器实现
8.set       底层数据结构为红黑树,有序,不重复
9.multiset  底层数据结构为红黑树,有序,可重复
10.map      底层数据结构为红黑树,有序,不重复
11.multimap 底层数据结构为红黑树,有序,可重复
12.hash_set 底层数据结构为hash表,无序,不重复
13.hash_multiset 底层数据结构为hash表,无序,可重复
14.hash_map      底层数据结构为hash表,无序,不重复
15.hash_multimap 底层数据结构为hash表,无序,可重复

from
http://blog.csdn.net/huangkq1989/article/details/7277282
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP