C++标准模板库(STL)——map常见用法详解
- map的定义
map<typename1, typename2> mp;
map需要确定映射前类型和映射后类型,所以需要在<>内填写两个类型,第一个是键的类型,第二个是值的类型。
注:如果是字符串到整型的映射,必须使用string而不能使用char数组。
map<string, int> mp;
- map容器内元素的访问
map可以通过下标访问或通过迭代器访问。
(1)通过下标访问
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 map<char, int> mp;
5 int main()
6 {
7 mp['a']=1;
8 mp['c']=2;
9 cout<<mp['c'];
10 return 0;
11 }
输出结果:
2
(2)通过迭代器访问
map迭代器的定义:
map<typename1, typename2>::iterator it;
map可以使用it->first来访问键,使用it->second来访问值。
实例:
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 map<char, int> mp;
5 int main()
6 {
7 mp['a']=1;
8 mp['c']=2;
9 mp['b']=3;
10 for(map<char, int>::iterator it=mp.begin();it!=mp.end();it++){
11 cout<<it->first<<" "<<it->second<<endl;
12 }
13 return 0;
14 }
输出结果:
a 1
b 3
c 2
从上面例子我们可以看出,map会以键从小到大的顺序自动排序。这是由于map内部是使用红黑树实现的(set也是)。
- map常用函数
(1)find()
find(key)返回键值为key的映射的迭代器,时间复杂度为O(logN),N为map中映射的个数。
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 map<char, int> mp;
5 int main()
6 {
7 mp['a']=1;
8 mp['c']=2;
9 mp['b']=3;
10 map<char, int>::iterator it=mp.find('c');
11 cout<<it->first<<" "<<it->second;
12 return 0;
13 }
输出结果:
c 2
(2)erase()
erase()可以删除单个元素,也可以删除一个区间的所有元素。
①删除单个元素
mp.erase(it),it为需要删除的元素的迭代器,时间复杂度为O(1)。
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 map<char, int> mp;
5 int main()
6 {
7 mp['a']=1;
8 mp['c']=2;
9 mp['b']=3;
10 map<char, int>::iterator it=mp.find('c');
11 mp.erase(it);
12 for(map<char, int>::iterator it=mp.begin();it!=mp.end();it++){
13 cout<<it->first<<" "<<it->second<<endl;
14 }
15 return 0;
16 }
输出结果:
a 1
b 3
mp.erase(key),key为欲删除的映射的键。时间复杂度为O(logN),N为map内元素的个数。
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 map<char, int> mp;
5 int main()
6 {
7 mp['a']=1;
8 mp['c']=2;
9 mp['b']=3;
10 mp.erase('c');
11 for(map<char, int>::iterator it=mp.begin();it!=mp.end();it++){
12 cout<<it->first<<" "<<it->second<<endl;
13 }
14 return 0;
15 }
输出结果:
a 1
b 3
②删除一个区间内的所有元素
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 map<char, int> mp;
5 int main()
6 {
7 mp['a']=1;
8 mp['c']=2;
9 mp['b']=3;
10 map<char, int>::iterator it=mp.find('c'); //令it指向键为c的值
11 mp.erase(it, mp.end()); //删除it之后的所有映射
12 for(map<char, int>::iterator it=mp.begin();it!=mp.end();it++){
13 cout<<it->first<<" "<<it->second<<endl;
14 }
15 return 0;
16 }
输出结果:
a 1
b 3
(3)size()
size()用来获得map中映射的对数,时间复杂度为O(1)。
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 map<char, int> mp;
5 int main()
6 {
7 mp['a']=1;
8 mp['c']=2;
9 mp['b']=3;
10 cout<<mp.size();
11 return 0;
12 }
输出结果:
3
(4)clear()
clear()用来清除map中的所有元素,时间复杂度为O(N)。
1 #include <iostream>
2 #include <map>
3 using namespace std;
4 map<char, int> mp;
5 int main()
6 {
7 mp['a']=1;
8 mp['c']=2;
9 mp['b']=3;
10 mp.clear(); //清空map
11 cout<<mp.size();
12 return 0;
13 }
输出结果:
0
C++标准模板库(STL)——map常见用法详解的更多相关文章
- C++标准模板库(STL)——queue常见用法详解
queue的定义 queue<typename> name; queue容器内元素的访问 由于队列本身就是一种先进先出的限制性数据结构,因此在STL中只能通过front()来访问队首元素, ...
- C++标准模板库(STL)——set常见用法详解
set的定义 set<typename> name; typename可以是任何基本类型,如int.double.char.结构体等,也可以是STL标准容器,如vector.set.que ...
- C++标准模板库(STL)——vector常见用法详解
vector的定义 vector<typename> name; 相当于定义了一个一维数组name[SIZE],只不过其长度可以根据需要进行变化,比较节省空间,通俗来讲,vector就是& ...
- STL map 常见用法详解
<算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...
- STL pair 常见用法详解
<算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...
- STL vector常见用法详解
<算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...
- STL stack 常见用法详解
<算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...
- STL priority_queue 常见用法详解
<算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...
- STL queue 常见用法详解
<算法笔记>学习笔记 queue 常见用法详解 queue翻译为队列,在STL中主要则是实现了一个先进先出的容器. 1. queue 的定义 //要使用queue,应先添加头文件#incl ...
随机推荐
- 批处理打造MySQLCleaner
#批处理打造MySQLCleaner ###1. 简介 在我们卸载MySQL数据库的时候,往往除了需要卸载软件,还需要删除各种注册表信息,隐藏文件,卸载服务,否则当我们再次安装MySQL时就会出现一些 ...
- maven下载Oracle jar包
Oracle的jar包由于是收费的,所以当我们使用maven去下载时下载不下来,对于这种情况,可以用以下方式去处理: oracle官网下载应用地址:https://www.oracle.com/dow ...
- 正则表达式:(mysql)REGEXP
检索列prod_name包含文本1000的所以行 SELECT prod_name FROM products WHERE prod_name REGEXP '1000' ORDER BY prod ...
- No input file specified.问题的解决
问题描述:apache配置网站出现问题"No input file specified." 解决1: 打开.htaccess 在RewriteRule 后面的index.php教程 ...
- CCNA 第一章 网络互联
1: 网络互联基础 互联网络定义:使用路由器将多个网络连接起来,并配置IP或者IPV6协议的逻辑网络编址方案,便组成了互联网络. 导致LAN(局域网)拥塞的常见原因: (1):广播域或者冲突域中的主机 ...
- 线程本地存储(动态TLS和静态TLS)
线程本地存储(TLS) 对于多线程应用程序,如果线程过于依赖全局变量和静态局部变量就会产生线程安全问题.也就是一个线程的使用全局变量可能会影响到其他也使用此全局变量的线程,有可能会造成一定的错误,这可 ...
- CSS中margin负值巧布局
margin负值实现细边框 我们先准备五个div盒子,并设置好浮动和2px的实线黑色边框,看看效果 中间的边框线挨在了一起致使边框变粗成了4px,这时使用margin负值就可以解决这个问题 <s ...
- 重新整理 .net core 实践篇————配置应用[一]
前言 本来想整理到<<重新整理.net core 计1400篇>>里面去,但是后来一想,整理 .net core 实践篇 是偏于实践,故而分开. 因为是重新整理,那么就从配置开 ...
- 如何提高CRM系统使用率?
随着时代的发展和市场的变化,客户在企业的眼中开始变得越来越重要.谁拥有更多的客户,谁就能在激烈的市场竞争中占据一席之地.现在很多企业通过CRM系统转变为了"以客户为中心".但是,许 ...
- Mybatis-plus在原有的select查询语句中动态追加查询条件
一.适用场景 1.使用了xml形式的mapper.2.不想在select查询中大量使用<if>标签来判断条件是否存在而加入条件. 二.步骤 1.自定义wrapper继承QueryWrapp ...