STL快速入门学习教程之map的简单使用
STL快速入门学习教程之map的简单使用
map是STL中的一个关联容器,它以一对一的数据进行整理(第一个数值称为关键字,且这个关键字只能在map中出现一次,第二个数值称为前关键字的值),正是由于这种特性,我们可以使用map处理一对一数据的时候的到快速的处理方式。map具有自动排序的功能,在map中的数据都是有序的。
1.map介绍
map是一个关联式的容器,它的特点是增加和删除节点时对迭代器的影响很小,除了操作的节点,对其他节点都不会产生什么影响,对于map容器来说,不能修改其key值,但key所对应的值可以修改。
2.map的性质
1.自动建立key与value的对应关系
2.key与value可以是你需要的任意数据类型(如:简单数据类型或者复合数据类型)
3.使用insert进行快速的插入Key和value的值
4.根据key的值进行快速的查找记录,查找的时间复杂度为log(N)
5.能进行快速的删除记录
6.根据key的值快速的修改value的值
7.和其他容器一样能遍历所有记录
3.使用原则
使用头文件#include< map>才能使用map类
map对象是模板类,需要使用关键字和存储对象两个模板参数:
map< int,string> number;
这样便定义了一个用int作为索引,并且拥有关联指向string的指针
4.构造函数
map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map:
map< int,string> mapstu;
5.数据的插入
三种插入数据的方法:
第一种:用insert函数插入pair数据
第二种:用insert函数插入value_type数据
第三种:用数组的方式插入数据
1.用insert函数插入pair数据
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map()
{map<int,string> stu;stu.insert(pair<int, string>(1001, "李华"));stu.insert(pair<int, string>(1002, "李明"));stu.insert(pair<int, string>(1003, "李强"));stu.insert(pair<int, string>(1004, "李平"));stu.insert(pair<int, string>(1005, "李雨"));for (map<int, string>::iterator it = stu.begin();it != stu.end();it++){cout << it->first<<" "<<it->second << endl;}
}
int main()
{init_map();system("pause");return 0;
}
运行结果:
2.用insert函数插入value_type数据
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map(map<int,string> &ma)
{map<int, string>::iterator it;for (it = ma.begin();it != ma.end();it++){cout << it->first << " " <<it->second<< endl;}
}
int main()
{map<int, string> stu;stu.insert(map<int, string>::value_type(1001, "李明"));stu.insert(map<int, string>::value_type(1002, "王明"));stu.insert(map<int, string>::value_type(1003, "张明"));stu.insert(map<int, string>::value_type(1004, "韦明"));stu.insert(map<int, string>::value_type(1005, "莫明"));init_map(stu);system("pause");return 0;
}
运行结果:
3.用数组的方式插入数据
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map(map<int, string> &ma)
{map<int, string>::iterator it;for (it = ma.begin();it != ma.end();it++){cout << it->first << " " << it->second << endl;}
}
int main()
{map<int, string> stu;stu[0] = { "晓华" };stu[1] = { "晓花" };stu[2] = { "晓强" };stu[3] = { "晓平" };stu[4] = { "晓明" };init_map(stu);system("pause");return 0;
}
运行结果:由结果可知,key值能够自动增加
虽然以上方式都能插入数据,但是它们的实现方法是有区别的,前二者方式基本一致,都使用insert函数进行数据的插入,但是特殊之处在于,插入数据时检测key值的唯一性,即当map中含有相应的key值时是无法插入数据的,但是数组插入的方式就不一样,它可以覆盖以前该关键字对应的值,无图无真相:
pair<map<int, string>::iterator, bool>pair_if; //用pair来获取24行是否插入成功map<int, string> stu;pair_if=stu.insert(map<int, string>::value_type(1001, "李明"));
数组插入的方式直接覆盖:
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map(map<int, string> &ma)
{map<int, string>::iterator it;for (it = ma.begin();it != ma.end();it++){cout << it->first << " " << it->second << endl;}
}
int main()
{map<int, string> stu;stu[0] = { "晓华" };stu[1] = { "晓花" };stu[2] = { "晓强" };stu[3] = { "晓平" };stu[4] = { "晓明" };stu[0] = { "晓雨" };init_map(stu);system("pause");return 0;
}
运行结果:
6.map的大小
map的元素个数:
int map_size=stu.size();
修改的main函数:
int main()
{map<int, string> stu;stu[0] = { "晓华" };stu[1] = { "晓花" };stu[2] = { "晓强" };stu[3] = { "晓平" };stu[4] = { "晓明" };stu[5] = { "晓雨" };int map_size = stu.size();init_map(stu);cout << "map的元素个数为:" << map_size << endl;system("pause");return 0;
}
结果:
7.数据的遍历
使用迭代器:
1.向前迭代器。其实前面我们使用的方式都是该种方式进行数据的遍历输出。
2.反向迭代器。具体看下面的代码操作:
修改如下:
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map()
{map<int,string> stu;stu.insert(pair<int, string>(1001, "李华"));stu.insert(pair<int, string>(1002, "李明"));stu.insert(pair<int, string>(1003, "李强"));stu.insert(pair<int, string>(1004, "李平"));stu.insert(pair<int, string>(1005, "李雨"));cout << "前向迭代器的输出结果如下:" << endl;for (map<int, string>::iterator it = stu.begin();it != stu.end();it++){cout << it->first<<" "<<it->second << endl;}cout << endl;cout << "反向迭代器的输出结果如下:" << endl;for (map<int, string>::reverse_iterator lit = stu.rbegin();lit != stu.rend();lit++){cout << lit->first << " " << lit->second << endl;}
}
int main()
{init_map();system("pause");return 0;
}
输出区别:
使用数组进行输出:
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map(map<int, string> &ma)
{map<int, string>::iterator it;for (it = ma.begin();it != ma.end();it++){cout << it->first << " " << it->second << endl;}
}
int main()
{map<int, string> stu;stu[0] = { "晓华" };stu[1] = { "晓花" };stu[2] = { "晓强" };stu[3] = { "晓平" };stu[4] = { "晓明" };stu[5] = { "晓雨" };int map_size = stu.size();cout << "使用迭代器输出内容:" << endl;init_map(stu);cout << "map的元素个数为:" << map_size << endl;cout << endl;cout << "使用数组进行map元素的输出:" << endl;for (int i = 0;i < map_size;i++){cout << stu[i] << endl;}system("pause");return 0;
}
结果:
数组反向遍历这里不再给出,同样使用数组下标索引值进行反向遍历即可。
8.判断map中的元素
在这里我们将体会,map在数据插入时保证有序的好处。
要判定一个数据(关键字)是否在map中出现的方法比较多,这里标题虽然是数据的查找,在这里将穿插着大量的map基本用法。
这里给出三种数据查找方法:
第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了
第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。
查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,
分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.
程序:
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map()
{map<int,string> stu;stu.insert(pair<int, string>(1001, "李华"));stu.insert(pair<int, string>(1002, "李明"));stu.insert(pair<int, string>(1003, "李强"));stu.insert(pair<int, string>(1004, "李平"));stu.insert(pair<int, string>(1005, "李雨"));//cout << "前向迭代器的输出结果如下:" << endl;//for (map<int, string>::iterator it = stu.begin();it != stu.end();it++)//{// cout << it->first<<" "<<it->second << endl;//}//cout << endl;//cout << "反向迭代器的输出结果如下:" << endl;//for (map<int, string>::reverse_iterator lit = stu.rbegin();lit != stu.rend();lit++)//{// cout << lit->first << " " << lit->second << endl;//}map<int, string>::iterator iter;iter = stu.find(1001);if (iter != stu.end()){cout << "Find, the value is " << iter->second << endl;}else{cout << "Do not Find" << endl;}}
int main()
{init_map();system("pause");return 0;
}
结果:
第三种方法:
lower_bound函数,这个函数用来返回要查找关键字的下界(是一个迭代器)
upper_bound函数,这个函数用来返回要查找关键字的上界(是一个迭代器)
例如:map中已经插入了1001,1002,1003,1004的话,如果lower_bound(1002)的话,返回的1002,而upper-bound(1002)的话,返回的就是1003
Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字,
程序:
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map(map<int, string> &ma)
{map<int, string>::iterator it;for (it = ma.begin();it != ma.end();it++){cout << it->first << " " << it->second << endl;}
}
int main()
{map<int, string> stu;//stu[0] = { "晓华" };stu[1] = { "晓花" };//stu[2] = { "晓强" };stu[3] = { "晓平" };//stu[4] = { "晓明" };stu[5] = { "晓雨" };int map_size = stu.size();//cout << "使用迭代器输出内容:" << endl;//init_map(stu);//cout << "map的元素个数为:" << map_size << endl;//cout << endl;//cout << "使用数组进行map元素的输出:" << endl;//for (int i = 0;i < map_size;i++)//{// cout << stu[i] << endl;//}map<int, string>::iterator iter;iter = stu.lower_bound(1); //返回的是下界1的迭代器 cout << iter->second << endl;iter = stu.lower_bound(2); //返回的是下界3的迭代器 cout << iter->second << endl;iter = stu.lower_bound(3); //返回的是下界3的迭代器cout << iter->second << endl;iter = stu.upper_bound(2); //返回的是上界3的迭代器 cout << iter->second << endl;iter = stu.upper_bound(3); //返回的是上界5的迭代器 cout << iter->second << endl;pair<map<int, string>::iterator, map<int, string>::iterator> mappair;mappair = stu.equal_range(2);if (mappair.first == mappair.second)cout << "找不到" << endl;elsecout << "找到了" << endl;mappair = stu.equal_range(3);if (mappair.first == mappair.second)cout << "找不到" << endl;elsecout << "找到了" << endl;system("pause");return 0;
}
结果:
9.从map中删除元素
移除某个map中某个条目用erase()
该成员方法的定义如下:
iterator erase(iterator it);//通过一个条目对象删除
iterator erase(iterator first,iterator last)//删除一个范围
size_type erase(const Key&key);//通过关键字删除
clear()就相当于enumMap.erase(enumMap.begin(),enumMap.end());
下面要用到erase函数,它有三个重载了的函数,下面在例子中详细说明它们的用法:
#include <iostream>
#include <map>
#include <string>using namespace std;void init_map()
{map<int,string> stu;stu.insert(pair<int, string>(1001, "李华"));stu.insert(pair<int, string>(1002, "李明"));stu.insert(pair<int, string>(1003, "李强"));stu.insert(pair<int, string>(1004, "李平"));stu.insert(pair<int, string>(1005, "李雨"));//使用迭代器删除map<int, string>::iterator iter;iter = stu.find(1001);stu.erase(iter);//使用关键字删除int n = stu.erase(1002);for (map<int, string>::iterator it = stu.begin();it != stu.end();it++){cout << it->first << " " << it->second << endl;}cout << "下面将会清空map" << endl;//用迭代器,成片的删除 //如下代码将map清空stu.erase(stu.begin(), stu.end());//cout << "前向迭代器的输出结果如下:" << endl;for (map<int, string>::iterator it = stu.begin();it != stu.end();it++){cout << it->first<<" "<<it->second << endl;}cout << endl;//cout << "反向迭代器的输出结果如下:" << endl;//for (map<int, string>::reverse_iterator lit = stu.rbegin();lit != stu.rend();lit++)//{// cout << lit->first << " " << lit->second << endl;//}//map<int, string>::iterator iter;//iter = stu.find(1001);//if (iter != stu.end())//{// cout << "Find, the value is " << iter->second << endl;//}//else//{// cout << "Do not Find" << endl;//}}
int main()
{init_map();system("pause");return 0;
}
结果:
10.map的基本操作函数
map的基本操作函数:
C++ maps是一种关联式容器,包含“关键字/值”对begin() 返回指向map头部的迭代器clear() 删除所有元素count() 返回指定元素出现的次数empty() 如果map为空则返回trueend() 返回指向map末尾的迭代器equal_range() 返回特殊条目的迭代器对erase() 删除一个元素find() 查找一个元素get_allocator() 返回map的配置器insert() 插入元素key_comp() 返回比较元素key的函数lower_bound() 返回键值>=给定元素的第一个位置max_size() 返回可以容纳的最大元素个数rbegin() 返回一个指向map尾部的逆向迭代器rend() 返回一个指向map头部的逆向迭代器size() 返回map中元素的个数swap() 交换两个mapupper_bound() 返回键值>给定元素的第一个位置value_comp() 返回比较元素value的函数
STL快速入门学习教程之map的简单使用相关推荐
- Qt 快速入门学习笔记
Qt 快速入门学习笔记 环境安装 环境配置以及安装 安装包下载地址 1.windows安装 msvc编译器模块需要安装Windows软件开发工具包. MinGW是Windows平台使用GNU工具导入库 ...
- STL教程:C++ STL快速入门
目录 1.STL引言 2.STL是什么(STL简介) 3.STL历史 4.STL组件 5.STL基本结构 6.STL 使用方法 7.STL目录 网址:STL教程:C++ STL快速入门(非常详细) 第 ...
- 虚幻引擎5:快速入门学习教程
流派:电子学习| MP4 |视频:h264,1280×720 |音频:AAC,48.0 KHz 语言:英语+中英文字幕(根据原英文字幕机译更准确)|大小解压后:5.63 GB |时长:5h 23m 从 ...
- 4. Vue入门实战教程之vue-element-admin后端API适配
Vue入门实战教程之vue-element-admin后端API适配 1.1 前言 1.2 分析框架 1.1.1 第一个接口:根据账号密码登陆获取token 1.1.2 第二个接口:根据token获取 ...
- Linux入门基础教程之Linux下软件安装
Linux入门基础教程之Linux下软件安装 一.在线安装: sudo apt-get install 即可安装 如果在安装完后无法用Tab键补全命令,可以执行: source ~/.zshrc AP ...
- java 怎么用 string method return数量_java教程之Map应该怎么用
java教程之Map应该怎么用 在自学或者看java教程的时候,大家多多少少都存在着这样或者那样的疑惑,对于这些中软国际小编一直也在整理,今天为大家献上的是关于Map应该怎样去用的一些方法,还是跟过去 ...
- Java快速入门学习笔记9 | Java语言中的方法
有人相爱,有人夜里开车看海,有人却连LeetCode第一题都解不出来!虽然之前系统地学习过java课程,但是到现在一年多没有碰过Java的代码,遇到LeetCode不知是喜是悲,思来想去,然后清空自己 ...
- Java快速入门学习笔记8 | Java语言中的数组
有人相爱,有人夜里开车看海,有人却连LeetCode第一题都解不出来!虽然之前系统地学习过java课程,但是到现在一年多没有碰过Java的代码,遇到LeetCode不知是喜是悲,思来想去,然后清空自己 ...
- Java快速入门学习笔记7 | Java语言中的类与对象
有人相爱,有人夜里开车看海,有人却连LeetCode第一题都解不出来!虽然之前系统地学习过java课程,但是到现在一年多没有碰过Java的代码,遇到LeetCode不知是喜是悲,思来想去,然后清空自己 ...
最新文章
- 图像理解之物体检测object detection,模型rcnn/fastrcnn/fasterrcnn原理及概念
- VS2015 编译输出的*.lib,*.dll,*.exe的区别
- 语音控制面板 通过linux_在Linux中,通常把设备作为( )来处理.
- HDU4622(后缀自动机)
- java数组 —(8)
- 信息学奥赛一本通 1062:最高的分数 | OpenJudge NOI 1.5 05
- C++ 工程实践(2):不要重载全局 ::operator new()
- oracle:oracle基础知识(一)
- python与冒泡排序
- 【追寻javascript高手之路05】理解事件流
- Vue:默认的App.vue
- 优品景象 进销存系统推荐_初步了解WMS(仓库管理系统)
- 驱动精灵w8ndows xp sp2,爱普生Epson TM-U220打印机驱动官方正式版下载,适用于winxp,winvista,win7,win8,win10-驱动精灵...
- 别在学习框架了,那些让你起飞的计算机基础知识。
- 【服务器配置】php move_uploaded_file文件移动失败
- Vue中实现将页面生成微信二维码
- 箭头函数写法_JavaScript|箭头函数的用法
- 信用社考试计算机知识点,2017年农村信用社计算机考试模拟题
- 很牛的运维必须精通Linux
- 计算机硬件的联通,联通4g支撑系统介绍_计算机硬件及收集_it计算机_专业资料[精彩].ppt...
热门文章
- 深度学习——学习率衰减(learning rate decay)
- java 计算某个时间段在多个时间段中分别用时
- 网上银行在计算机应用中,基于软件工程和J2EE的网上银行系统实现计算机应用技术专业论文.docx...
- Java中 String s = new String(“hello“)和String s = “hello“的区别
- 安卓手机连接IP100蓝牙打印机实现打印功能
- 大尺寸卫星图像目标检测:yoloT
- JS实现简单的网页文本转语音阅读
- “易语言.飞扬”十分钟入门教程
- run vue task的项目报错:Error while running task C:\IT\xxxxxx:serve with message‘spawn vue-cli-service
- RHEL8.0快速入门系列笔记--理论知识储备(一)