C++ map详解
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详解的更多相关文章
- Sass map详解
作为一个CSS预处理器,Sass正受到越来越多的青睐,诸如Github.Codepen.CSS-Tricks.SitePoint.w3cplus等网站采用Sass组织.管理CSS文件,Sass正在逐渐 ...
- List、Set、Map详解及区别
一.List接口 List是一个继承于Collection的接口,即List是集合中的一种.List是有序的队列,List中的每一个元素都有一个索引:第一个元素的索引值是0,往后的元素的索引值依次+1 ...
- 源映射(Source Map)详解
一.什么是源映射 为了提高性能,很多站点都会先压缩 JavaScript 代码然后上线, 但如果代码运行时出现错误,浏览器只会显示在已压缩的代码中的位置,很难确定真正的源码错误位置. 这时源映射就登场 ...
- java中list和map详解
一.概叙 List , Set, Map都是接口,前两个继承至Collection接口,Map为独立接口, List下有ArrayList,Vector,LinkedList Set下有HashSet ...
- Array.prototype.map()详解
今天在地铁上看到这样一个小例子: ["1","2","3"].map(parseInt); 相信很多人和我一样,觉得输出的结果是[1,2,3 ...
- python 中的map 详解
python中的map函数应用于每一个可迭代的项,返回的是一个结果list.如果有其他的可迭代参数传进来,map函数则会把每一个参数都以相应的处理函数进行迭代处理.map()函数接收两个参数,一个是函 ...
- 集合框架学习之Collection和Map详解
线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以 ...
- Javascript中Array.prototype.map()详解
map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...
- JavaScript Source Map 详解
源码地址: http://www.ruanyifeng.com/blog/2013/01/javascript_source_map.html 上周,jQuery 1.9发布. 这是2.0版之前的最后 ...
随机推荐
- CPU MPU MCU SOC SOPC关系及区别
在嵌入式开过程,会经常接触到一些缩写术语概念,这些概念在嵌入式行业中使用率非常高,下面我们就解释一下这些概念之间的关系和区别: 1.CPU(Central Processing Unit),是一台计算 ...
- 使用 VisualVM 进行性能分析及调优
VisualVM 是一款免费的性能分析工具.它通过 jvmstat.JMX.SA(Serviceability Agent)以及 Attach API 等多种方式从程序运行时获得实时数据,从而进行动态 ...
- (转)HTML5开发学习(2):本地存储之localStorage 、sessionStorage、globalStorage
原文:http://www.cnblogs.com/xumingxiang/archive/2012/03/25/2416386.html HTML5开发学习(2):本地存储之localStorage ...
- Spring中@Resource、@controller注解的含义
@Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象. Spring确实支 ...
- 算法题----称硬币: 2n(并不要求n是2的幂次方)个硬币,有两个硬币重量为m+1, m-1, 其余都是m 分治 O(lgn)找出假币
Description: 有2n个硬币和一个天平,其中有一个质量是m+1, 另一个硬币质量为m-1, 其余的硬币质量都是m. 要求:O(lgn)时间找出两枚假币 注意: n不一定是2的幂次方 算法1: ...
- ERP部门的添加(十一)
功能需求: 1.有部门管理权限的人员进行添加部门基本信息. 2.有部门权限管理的人员查询部门基本信息. 3.有部门权限管理的人员进行修改部门基本信息. 4.在一个页面中实现,使用弹出对话框方式 存储过 ...
- mysql实现高效率随机取数据
从数据库中(mysql)随机获取几条数据很简单,但是如果一个表的数据基数很大,比如一千万,从一千万中随机产生10条数据,那就相当慢了,如果同时一百个人访问网站,处理这些个进程,对于一般的服务器来说,肯 ...
- [转]iOS游戏如何防御外挂及IAP破解
http://www.j1f3.com/news/game/21371.html 今年3月初写过一篇<iO平台游戏安全小议>,到现今已有7个月了.在这段时间内,iOS平台上的安全问题也产生 ...
- 转:通过代码理解Asp.net4中的几种ClientIDMode设置.
转:http://www.cnblogs.com/xray2005/archive/2011/07/05/2097881.html 以前我们可以通过ClientID在JavaScript脚本中服务器端 ...
- platanus
nohup platanus assemble -o Larrrea -f ../unknown_NoIndex_L000_R1.fastq ../unknown_NoIndex_L000_R2.f ...