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版之前的最后 ... 
随机推荐
- uart与usart
			字面意义:UART:universal asynchronous receiver and transmitter通用异步收发器:USART:universal synchronous asynchr ... 
- Iterator之java.util.ConcurrentModificationException
			在运行以下代码时,会报java.util.ConcurrentModificationException异常, public class Demo { public static void main( ... 
- redis补充和rabbitmq讲解
			线程池: redis发布订阅: rabbitMQ: MySQL; python pymysql: python orm SQLAchemy: paramiko: 堡垒机: 1.线程池 1.1 cont ... 
- 文本编辑器vim
			Vim是一个功能强大的全屏幕文本编辑器,是Linux/UNIX上最大的文本编辑器,它的作用是建立.编辑.显示文本文件. Vim没有菜单,只有命令. 
- iOS:开发者中心证书创建流程
			一,首先点击开发者首页(https://developer.apple.com/)里面的Member Center.二,输入开发者账号和密码,点击sign in登录.三,点击Certificates, ... 
- 进程同步(二)—— 信号量&内存共享
			内存共享是进程间常用的通信方式,可以实现两个完全独立的进程通信. 在访问共享内存时,同时需要信号量进行访问控制. 使用ipcs -m命令可以查看系统共享内存,ipce -m + key 可以删除指定的 ... 
- html5in24hours
			http://www.html5in24hours.com/books/the-book/code/ 1.浏览器测试 http://browsershots.org/ 2.http://www.fon ... 
- 谷歌、火狐浏览器gift图片缓存后不显示动态效果
			<script> $(function(){ $('.center img').prop("src","images/service/01.gif" ... 
- Ext.Net系列:一安装与使用
			http://www.cnblogs.com/howDo/archive/2011/04/27/2031084.html 下载地址:http://www.ext.net/download/ 示例地址: ... 
- oracle将多条数据合并成一条
			select t.col1,t.col2 ,wmsys.wm_concat(t.col3||' ') AS col3 , wmsys.wm_concat(t.col4||'') as col4,w ... 
