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版之前的最后 ...
随机推荐
- c规范(1)
1文件结构 头文件.h 保存文件声明 定义文件.c 程序实现 2版本标示 用注释 (1)版权信息. (2)文件名称,标识符,摘要. (3)当前版本号,作者 修改者,完成日期. (4)版本历史信息. ...
- python中的函数调用绑定,静态方法和类方法
在C++的类中,有两种函数:普通成员函数和静态成员函数,差别是成员函数通过类实例调用,而静态成员函数通过类名调用.本质上,成员函数在调用的时候会默认把this指针作为第一个参数传入,而静态成员函数不需 ...
- [zz]Java中的instanceof关键字
1.What is the 'instanceof' operator used for? stackoverflow的一个回答:http://stackoverflow.com/questions/ ...
- 【LeetCode OJ】Word Break
Problem link: http://oj.leetcode.com/problems/word-break/ We solve this problem using Dynamic Progra ...
- 如何从oc中去获取一个私有的变量.....
运行时 的用法 1.定义的一个类,里面有一个私有变量mt_,并且在初始化值为"HaHa Ha ".@interface Mobj : NSObject {@privateNSStr ...
- PHP CI分页类带多个参数
通过修改system中的pagination.php,给每个<a>都增加了class="pagination". view页面 <div class=" ...
- PHP url重定向
1.使用header()函数 PHP的 HTTP相关函数种提供了一个 header()函数,首先要清楚,header()函数必须放在php程序的开头部分,而且之前不能有另外的 header() 函数或 ...
- HDU 4704
http://acm.hdu.edu.cn/showproblem.php?pid=4704 求(2^n)%mod的方法 #include <iostream> #include < ...
- Golang Deco Enco
mproto.go package mproto import ( "bytes" "encoding/binary" "fmt" &quo ...
- mysql重命名数据表
命令如下: rename table oldtablename to newtablename