1.什么是map
map是一个键值对容器。在处理一对一数据是,很有用。

2.map数据结构的特点
map内部自建一颗红黑树,这棵树具有对数据自动排序的功能,
因此,map内的数据都是按key的值排好序的。

3.map数据插入
数据的插入有三种方法:
第一种,调用insert函数,插入pair类型数据
示例如下所示:

#include <map>
#include <string>
#include <iostream>
Using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(, “student_one”));
mapStudent.insert(pair<int, string>(, “student_two”));
mapStudent.insert(pair<int, string>(, “student_three”));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<” ”<<iter->second<<end;
}
return ;
}

第二种,调用insert插入value_type类型数据
示例如下所示:

#include <map>
#include <string>
#include <iostream>
Using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type(, “student_one”));
mapStudent.insert(map<int, string>::value_type(, “student_two”));
mapStudent.insert(map<int, string>::value_type(, “student_three”));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<” ”<<iter->second<<end;
}
return ;
}

第三种,使用数组赋值方式

#include <map>
#include <string>
#include <iostream>
Using namespace std;
int main()
{
map<int, string> mapStudent;
mapStudent[]="student_one";
mapStudent[]="student_two";
mapStudent[]="student_three";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<” ”<<iter->second<<end;
}
return ;
}

如何判断数据是否插入成功?
insert函数的返回值类型为:Pair<map<int,string>::iterator,bool>
返回值的key值表示,返回map的迭代器
返回值的value值表示,是否插入成功
因此,我们可以使用以下代码来判断,map键值对是否插入成功

Pair<map<int,string>::iterator,bool> Insert_Pair=mapStudent.insert(pair<int, string>(, “student_one”));
if(Insert_Pair.second)
cout<<"success.\n";
else
cout<<"failed.\n";

4.map数据查找
第一种,使用count方法判定是否存在
第二种,使用find方法,定位key出现的位置,该方法返回一个迭代器。
当数据出现时,返回数据所在位置的迭代器;
否则,返回的迭代器等于end方法返回的迭代器。
示例代码如下:

map<int,string>::iterator iter;
iter=mapStudent.find();
if(iter!=mapStudent.end())
cout<<"success find.\n";
else
cout<<"failed.\n";

第三种,使用Lower_bound,Upper_bound方法,返回key的边界,在此,不再详细介绍。

5.map数据删除
使用了我们很熟悉的erase函数,map中该函数有三个重载。
1)使用迭代器删除

map<int,string>::iterator iter;
iter = mapStudent.find();
mapStudent.erase(iter);

2)使用关键字key删除

int result = mapStudent.erase();

成功返回1,否则返回0

3)使用迭代器,删除区间内的数据

mapStudent.erase(mapStudent.begin(),mapStudent.end());

6.关于map的自动排序
前面我们说过,map内的数据会根据key值由大到小排序,
也就是说key值必须支持小于<运算,否则无法插入map。
对于上面的示例,我们知道int类型本身是支持小于运算的。
但是对于不支持小于运算的key类型,我们该如何插入map呢?
很显然,我们需要自定义该类型的<操作符。
如下例所示,我们要建立一个map<学生信息,分数>:

typedef struct tagStudentInfo
{
int nID;
string strName;
bool operator<(tagStudentInfo const &_A) const
{
if(nID<_A.nID)
return true;
return false;
}
}StudentInfo,*PStudentInfo;
int main()
{
map<StudentInfo,int> mapStudent;
StudentInfo studentInfo;
studentInfo.nID=;
studentInfo.strName="student_one";
mapStudent.insert(pair<StudentInfo, int>(studentInfo,));
return ;
}

另外一种方式是,定义一个单独的类,类中定义key类型的比较函数
示例代码如下:

typedef struct tagStudentInfo
{
int nID;
string strName; }StudentInfo,*PStudentInfo;
class sort
{
public:
bool operator()(tagStudentInfo const &_A,tagStudentInfo const &_B) const
{
if(_A.nID<_B.nID)
return true;
return false;
}
}
int main()
{
map<StudentInfo,int,sort> mapStudent;
StudentInfo studentInfo;
studentInfo.nID=;
studentInfo.strName="student_one";
mapStudent.insert(pair<StudentInfo, int>(studentInfo,));
return ;
}

7.map内存占用
前面我们介绍过,map的数据结构为一颗红黑树,
该树的一个节点在不保存数据时,占用16字节的空间,
包括一个父节点指针,左右孩子指针,还有一个枚举值(标示红黑的,相当于平衡二叉树中的平衡因子),
可见,map还是很耗内存的。

C++ map详解的更多相关文章

  1. Sass map详解

    作为一个CSS预处理器,Sass正受到越来越多的青睐,诸如Github.Codepen.CSS-Tricks.SitePoint.w3cplus等网站采用Sass组织.管理CSS文件,Sass正在逐渐 ...

  2. List、Set、Map详解及区别

    一.List接口 List是一个继承于Collection的接口,即List是集合中的一种.List是有序的队列,List中的每一个元素都有一个索引:第一个元素的索引值是0,往后的元素的索引值依次+1 ...

  3. 源映射(Source Map)详解

    一.什么是源映射 为了提高性能,很多站点都会先压缩 JavaScript 代码然后上线, 但如果代码运行时出现错误,浏览器只会显示在已压缩的代码中的位置,很难确定真正的源码错误位置. 这时源映射就登场 ...

  4. java中list和map详解

    一.概叙 List , Set, Map都是接口,前两个继承至Collection接口,Map为独立接口, List下有ArrayList,Vector,LinkedList Set下有HashSet ...

  5. Array.prototype.map()详解

    今天在地铁上看到这样一个小例子: ["1","2","3"].map(parseInt); 相信很多人和我一样,觉得输出的结果是[1,2,3 ...

  6. python 中的map 详解

    python中的map函数应用于每一个可迭代的项,返回的是一个结果list.如果有其他的可迭代参数传进来,map函数则会把每一个参数都以相应的处理函数进行迭代处理.map()函数接收两个参数,一个是函 ...

  7. 集合框架学习之Collection和Map详解

    线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以 ...

  8. Javascript中Array.prototype.map()详解

    map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...

  9. JavaScript Source Map 详解

    源码地址: http://www.ruanyifeng.com/blog/2013/01/javascript_source_map.html 上周,jQuery 1.9发布. 这是2.0版之前的最后 ...

随机推荐

  1. powerdesigner的学习

    1  powerdesigner的层次 使用过eclipse作为java开发的都知道,workspace是顶级目录,一次只能打开一个,project是项目,位于workspace中,一个workspa ...

  2. svnUbuntu下安装与使用(ZT)

    1.安装包 1.$ sudo apt-get install subversion 2.创建项目目录 $ sudo mkdir /home/xiaozhe/svn $ cd /home/xiaozhe ...

  3. 建置 POSTFIX 服务器

    建置 POSTFIX 服务器 postfix 是除了 sendmail 以外 ,最被广泛采用的 Linux 邮件服务器,一般使用的观感不外乎两点: 一.安全:垃圾信过滤机制较聪明,就算什么都没设定,也 ...

  4. vs常用调试快捷键

    vs2005常用调试快捷键 ,开发起来更加的方面,虽然现在vs2008发布了,但vs2005还是一个主流,个人还是用vs2005,调试代码也多. F6: 生成解决方案Ctrl+F6: 生成当前项目F7 ...

  5. 【转】DOM事件简介

    原文转自:http://blog.jobbole.com/52430/ Click.touch.load.drag.change.input.error.risize — 这些都是冗长的DOM(文档对 ...

  6. 应用Druid监控SQL语句的执行情况(转)

    Druid是什么? Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBo ...

  7. sudo: 无法解析主机:<主机名>

    hostname配置出错 解决方法:进入目录 /etc/hosts编辑,命令 sudo gedit /etc/hosts 在其中加入下面行:127.0.0.1 主机名

  8. Lumia刷机Win10 Mobile 10.0.10166惊魂记

    1 手贱,不愿等正式版正式发布,结果手动更新了,为此还熬了两个晚上. 2 第一次撞上去,没有无线了,倒.一开始还以为是预览版BUG,后来查了下重置就可以了,可以没有3G,没有WIFI也没办法备份,干, ...

  9. 2015GitWebRTC编译实录

    整体解决思路1 编译完成后2~3天,对之前编译的lib库进行测试.目前阶段至少保证真机测试是ok的,模拟器先放到一边.2015.06.24完成编译状况编译完成libjsoncpp编译完成libsyst ...

  10. 转:【Spring MVC Controller单例陷阱】

    http://lavasoft.blog.51cto.com/62575/1394669/ Spring MVC Controller默认是单例的: 单例的原因有二:1.为了性能.2.不需要多例. 1 ...