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. NorFlash和NandFlash区别

      Flash编程原理都是只能将1写为0,而不能将0写成1.所以在Flash编程之前,必须将对应的块擦除,而擦除的过程就是将所有位都写为1的过程,块内的所有字节变为0xFF.因此可以说,编程是将相应位 ...

  2. mongo db 分享 ppt

    在公司内部的mongo db的ppt.初步进阶 http://files.cnblogs.com/files/yuhan-TB/mongoDB.pptx

  3. HttpApplication 类,HttpApplicationState 类

    HttpApplication 类 定义 ASP.NET 应用程序中的所有应用程序对象共有的方法.属性和事件.此类是用户在 Global.asax 文件中所定义的应用程序的基类. https://ms ...

  4. php生成图片

    //生成图片,第一个参数宽,第二个高 1.$image = imagecreatetruecolor(100,25); //生成颜色,当第一次调用生成颜色的方法,是生成背景颜色 2.$backgrou ...

  5. Oracle常用SQL语句

    --2.查看表结构 desc wx_monitor_excption; --3.从表中查询数据 select * from wx_monitor_excption; --7.双引号保持原来的格式 se ...

  6. GIT之一 起步篇

    关于版本控制 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.如果你是位图形或网页设计师,可能会需要保存某一幅图片或页面布局文件的所有修订版本.采用版本控制系统(VCS) ...

  7. 11g_job执行带参数的procedure

    现有一个参数为date类型的存储:   create or replace procedure t_pro(t_var in date) as begin dbms_output.put_line(T ...

  8. 深入分析Php处理浮点数的问题

    下文来为各位介绍Php处理浮点数的问题了,如果各位在使用过程中碰到这些问题我们可以一起来看看,希望文章对各位有帮助 公司要对产品价格做调整,因为做的外贸商城,所以价格要和国际接轨.比如国外的价格展示方 ...

  9. Magento请求分发与控制器

    Magento请求分发与控制器 Magento使用的是MVC结构,模型-试图-控制器结构,这样更好的实现显示逻辑和数据,业务逻辑的分离,更好的适合开发! 下面为传统的mvc结构 URL请求被一个PHP ...

  10. LeetCode Find the Duplicate Number 找重复出现的数(技巧)

    题意: 有一个含有n+1个元素的数组,元素值是在1-n之间的整数,请找出其中出现超过1次的数.(保证仅有1个出现次数是超过1的数) 思路: 方法一:O(nlogn).根据鸽笼原理及题意,每次如果< ...