在map中插入元素

改变map中的条目非常简单,因为map类已经对[]操作符进行了重载

enumMap[1] = "One";
enumMap[2] = "Two";
.....

这样非常直观,但存在一个性能的问题。插入2时,先在enumMap中查找主键为2的项,没发现,然后将一个新的对象插入enumMap,键是2,值是一个空字符串,插入完成后,将字符串赋为"Two"; 该方法会将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。我们可以用以下方法来避免开销:

enumMap.insert(map<int, CString> :: value_type(2, "Two"))

insert()方法:若插入的元素的键值已经存在于map中,那么插入就会失败,不会修改元素的键对应的值;若键值在map中查不到,那么就会将该新元素加到map中去。

下标[key]方法:若插入元素的键值已经存在于map中,那么会更新该键值对应的值为新的元素的值;若该键值在map中找不到,那么就会新建一个键值为该键(key)的元素,并将key对应的值赋值为默认值(默认构造函数生成的对象)。

直接上代码,两种方式:

map<string,int> m_map;

  1. m_map.insert(map<string,int>::value_type("hello",5));
  2. m_map.insert(make_pair("hello",5));

也就是说,insert后面的数据是pair类型或者是value_type类型了,然而对C++有了解的人都明白,其实value_type和pair<const k,v>是等价的、insert()中的参数必须是value_type类型,那么为什么insert()中的参数能够使用make_pair产生的pair<k,v>呢?

其实,因为我们在加入pair<k,v>时的k已经是常量了,所以可以加入。。。而正常来讲这都是所有编译器所能接受的。

在insert插入的同时,还有返回值来说明是否插入成功,就是pair< map<string,int>::iterator,bool> >类型,如本实例pair< map<string,int>::iterator,bool> > rent= m_map.insert(make_pair("hello",5));

rent->second即是成功与否的标志;rent->first就是返回的map<string,int>::iterator迭代器;rent->first.first就是string类型的数据。

这些看起来都非常的麻烦,但是只要信心的研究、编译、调试就可以了。

std::map的insert和下标[]访问的更多相关文章

  1. 有关std::map和std::vector的使用

    先说map吧. 最需要注意的就是:用下标访问map中的元素时,与使用下标访问vector的行为截然不同! 用下标访问不存在的元素时,将导致在map容器中添加一个新的元素,它的键即为该下标! 然而很多时 ...

  2. C++ std::map用法简介

    #include "map" //引入头文件 初始化: std::map <int, std::string> _map1; //初始化 //c++11中引入的,可以直 ...

  3. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  4. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  5. C++ std::map::erase用法及其陷阱

    1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string ...

  6. std::map

    1.例: map<int,string> m_mapTest; m_mapTest.insert(make_pair(1,"kong")); m_mapTest.ins ...

  7. std::map的clear()没有用?

    昨天晚上,我徒弟跑过来讲,他的程序的内存占用居高不下,愿意是std::map的clear()没有效果.于是我让他用erase(begin,end); 试试也不行. 代码如下: void release ...

  8. std::map的操作:插入、修改、删除和遍历

    using namespace std; std::map<int,int> m_map; 1.添加 for(int i=0;i<10;i++) { m_map.insert(mak ...

  9. Using std::map with a custom class key

    From: https://www.walletfox.com/course/mapwithcustomclasskey.php If you have ever tried to use a cus ...

随机推荐

  1. java普通类如何得到spring中的bean类

    在SSH集成的前提下.某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象. 之前我在网上找了好几好久都没有找到合适的方法.例如: ApplicationContext ...

  2. project euler 26:Reciprocal cycles

    A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with d ...

  3. pubwin2009服务端 修改系统时间方法

      所有操作都是必须的,包括重启. 1.建议操作前全体结账,并交班---不结帐的话, 改完时间会出问题的.2.停止pubwin2009服务端 3.将C:\WINDOWS\system32\driver ...

  4. static的用法解析

    PHP中static变量的使用范围要更广一些,我们不仅可以在类,方法或变量前面添加static修饰符,我们甚至还能给函数内部变量添加static关键字.添加了static修饰符的变量即使在该函数执行完 ...

  5. IOS深入学习(4)之Coordinate System

    1 前言 在IOS中相信大家会经常跟一些bounds,frame之类的打交道,这不免会涉及坐标系统,今天我们就来介绍一下Coordinate System(坐标系). 2 详述 坐标系统是定位,大小, ...

  6. 动态绑定ReportViewer控件之经验总结

    以上两篇文章已经很丰富了,但是照做一遍不行,检查了N遍还是不行,就是找不出问题原因,总是提示“尚未为数据源“DataSet1_DataTable1”提供数据源实例.”这主要是说在为ReportView ...

  7. Qt Creator编译时:cannot open file 'debug\QtGuiEx.exe' File not found

    Qt Creator编译时:cannot open file 'debug\QtGuiEx.exe' File not found 利用Qt Creator编译工程时,出现如题目所示的错误,其中红色部 ...

  8. 异常:ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed...

    ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed: com.movie.类 放到lib 包下 \W ...

  9. 【sql语句】好用的sql语句之项目数据库学习总结

    转载请注明出处:http://blog.csdn.net/pearyangyang/article/details/41115491 这几天学习公司系统的数据流向.主要涉及到几个表的数据. 可是表中的 ...

  10. Alter的用法(添加字段,删除字段,修改字段名)

    1.在表emp中新增字段sexy(性别) alter table emp add sexy varchar2(2); 新增多个字段cxx 和shoneworn alter table emp add  ...