在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. windows下配置Nginx+Mysql+Php7

    环境:Windows10 mysql-5.6.24-win32解压缩版    nginx-1.8.0    php7 1.Mysql安装 下载压缩文件之后解压缩至相应目录(我的目录是G:\wnmp\m ...

  2. typecho博客出404页面修改方法

    适用于typecho博客版本为:0.9 (13.12.12) typecho博客,很多时候可能安装完毕,除了首页,其他页面都是404=.= 在匹配*.php的location区域修改为以下格式: lo ...

  3. Hibernate中3种结果转换的详细说明(转)

    Hibernate中3种结果转换的详细说明 在hibernate使用的过程中.我们通常需要对结果进行解释. Hibernate为我们提供了以下3种解释方法: Transformers.ALIAS_TO ...

  4. js 图片无缝循环

    <html> <head> <title>Js图片无缝滚动</title> <style type="text/css"> ...

  5. slime+sbcl for common lisp

    sudo apt-get install slime audo apt-get install sbcl ;;sbcl+slime for common lisp ;;sudo apt-get ins ...

  6. python爬虫系列之爬京东手机数据

    python抓京东手机数据 作者:vpoet mail:vpoet_sir@163.com #coding=utf-8 import urllib2 from lxml import etree im ...

  7. 阿里云主机和RDS使用心得

    本文上非广告,只是将这近1年的使用过程给大家分享一下. 去年下半年,服务器托管到期,加上服务器也使用了5.6年,严重老化,当时正好看到阿里云的宣传广告,就开始了阿里云使用历程.陆续购买了4台云主机,I ...

  8. hdu1556 Color the ball

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  9. openssl 生成CSR

    openssl 生成CSR 2013-12-27 15:05 3699人阅读 评论(1) 收藏 举报  分类: Security(38)  C/C++(105)  版权声明:本文为博主原创文章,未经博 ...

  10. GCD 和延时调用

    因为 Playground 不进行特别配置的话是无法在线程中进行调度的,因此本节中的示例代码需要在 Xcode 项目环境中运行.在 Playground 中可能无法得到正确的结果. GCD 是一种非常 ...