map用法38335

map用法38335

ID:19826640

大小:72.00 KB

页数:17页

时间:2018-10-06

map用法38335_第1页
map用法38335_第2页
map用法38335_第3页
map用法38335_第4页
map用法38335_第5页
资源描述:

《map用法38335》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、MapmapStudent;1.      map的构造函数map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map:MapmapStudent;2.      数据的插入在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法:第一种:用insert函数插入pair数据,下面举例说明(以下代码虽然是随手写的,应该可以在VC和GCC下编译通过,大家可以运行下看什么效果,在VC下请加入这条语句,

2、屏蔽4786警告 #pragmawarning(disable:4786))#include#include#includeUsingnamespacestd;Intmain(){      MapmapStudent;      mapStudent.insert(pair(1,“student_one”));      mapStudent.insert(pair(2,“student_two”));      mapStudent.i

3、nsert(pair(3,“student_three”));      map::iterator iter;      for(iter=mapStudent.begin();iter!=mapStudent.end();iter++){      Cout<first<<”  ”<second<#include#includeUsin

4、gnamespacestd;Intmain(){      MapmapStudent;      mapStudent.insert(map::value_type(1,“student_one”));      mapStudent.insert(map::value_type(2,“student_two”));      mapStudent.insert(map::value_type(3,“student_three”));      map

5、,string>::iterator iter;      for(iter=mapStudent.begin();iter!=mapStudent.end();iter++){      Cout<first<<”  ”<second<#include#includeUsingnamespacestd;Intmain(){      MapmapStudent;      mapStude

6、nt[1]= “student_one”;      mapStudent[2]= “student_two”;      mapStudent[3]= “student_three”;      map::iterator iter;      for(iter=mapStudent.begin();iter!=mapStudent.end();iter++){      Cout<first<<”  ”<second<

7、种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明mapStudent.insert(map::value_type(1,“student_one”));mapStudent.insert(map::value_type(1,“student_two”));上面这两条语句执行后,map中1这个关键字对应的值是“student_one

8、”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。