标准库 map set 大锅炖
一,关联容器有哪些
| 按关键字有序保存元素 | |
|---|---|
| map | 保存key和value |
| set | 只保存key |
| mulutimap | key可以重复出现 |
| multiset | key可以重复出现 |
| 无序集合 | |
| unordered_map | 用哈希函数做成的map |
| unordered_set | 用哈希函数做成的set |
| unordered_mulutimap | key可以重复出现 |
| unordered_multiset | key可以重复出现 |
二,关联容器的类型别名
| 类型别名 | 功能描述 |
|---|---|
| key_type | 关键字(key)的类型 |
| value_type | set的话,和key_type一样;map的话,是个pair |
| mapped_type | 只适用于map,值(value)的类型 |
set<string>::value_type v1(\"aa\");//v1是string类型
set<string>::key_type v2(\"bb\");//v2是string类型
map<string, int >::value_type v3{\"aa\",12};//v3是pair<const string,int>类型
map<string, int>::key_type v4(\"cc\");//v4是string类型
//只有map才有mapped_type
map<string, int>::mapped_type v5(100);//v5是int类型
三,关联容器的迭代器
当解一个关联容器的迭代器,会得到一个类型为容器的value_type的值的引用。
- 解一个map的迭代器,得到的是pair,key是const的,不可以更改。
- 解一个set的迭代器,得到的是key也是const的,不可以更改。
map<string, int> cnt{{\"aa\",1}, {\"bb\",2}};
auto map_it = cnt.begin();
//map_it->first = \"new key\";//错误,first为const
++map_it->second;
cout << map_it->second << endl;//2
set<int> iset{1,2,2,3,3,5};
set<int>::iterator set_it = iset.begin();
//*set_it = 10;//错误,不可以改变set的key
四,遍历关联容器
当使用迭代器遍历map,multimap,set,multiset时,迭代器按关键字升序遍历元素。
map<string, int> cnt{{\"dd\",1}, {\"cc\",2},{\"aa\", 10}};
auto map_it = cnt.cbegin();
while(map_it != cnt.cend()){
cout << map_it->first << \":\" << map_it->second << endl;
++map_it;
}
for(auto &s : cnt){
cout << s.first << \":\" << s.second << endl;
}
五,对关联容器使用泛型算法
- 通常不对关联容器使用泛型算法,因为关联容器有key为const特性,很多算法不适用
- 关联容器可用于只读算法,但是这些算法都需要搜索序列,比如find算法。但是关联容器有关键字,所以还莫不如用关联容器自己的find成员方法来的效率高。
- 实际中,一般对关联容器,使用copy算法比较多
multiset<string> c{\"aa\",\"aa\",\"dd\",\"cc\"};
vector<string> v{\"ee\",\"ff\"};
//copy(v.begin(),v.end(), inserter(c, c.end()));//OK
//copy(v.begin(),v.end(), back_inserter(c));//NG,关联容器没有push_back
//copy(c.begin(),c.end(), inserter(v, v.end()));//OK,并且c的begin到end时拍好序的
copy(c.begin(),c.end(), back_inserter(v));//OK
for(auto &s : c){
cout << s << \" \";
}
cout << endl;
for(auto &s : v){
cout << s << \" \";
}
cout << endl;
小例子索引
| 代码块 | 功能描述 |
|---|---|
| test1 | 关联容器的别名 |
| test2 | 解关联容器的迭代器 |
| test3 | 遍历关联容器 |
| test4 | 对关联容器通用算法 |
小例子:
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
int main(){
//test1 关联容器的别名
/*
set<string>::value_type v1(\"aa\");//v1是string类型
set<string>::key_type v2(\"bb\");//v2是string类型
map<string, int >::value_type v3{\"aa\",12};//v3是pair<const string,int>类型
map<string, int>::key_type v4(\"cc\");//v4是string类型
//只有map才有mapped_type
map<string, int>::mapped_type v5(100);//v5是int类型
*/
//test2 解关联容器的迭代器
/*
map<string, int> cnt{{\"aa\",1}, {\"bb\",2}};
auto map_it = cnt.begin();
//map_it->first = \"new key\";//错误,first为const
++map_it->second;
cout << map_it->second << endl;//2
set<int> iset{1,2,2,3,3,5};
set<int>::iterator set_it = iset.begin();
//*set_it = 10;//错误,不可以改变set的key
*/
//test3 遍历关联容器
/*
map<string, int> cnt{{\"dd\",1}, {\"cc\",2},{\"aa\", 10}};
map<string, int>::const_iterator map_it = cnt.cbegin();
while(map_it != cnt.cend()){
cout << map_it->first << \":\" << map_it->second << endl;
++map_it;
}
for(auto &s : cnt){
cout << s.first << \":\" << s.second << endl;
}
auto it = cnt.begin();
pair<string, int> p1 = *it;
*/
//test4 关联容器的通用算法
multiset<string> c{\"aa\",\"aa\",\"dd\",\"cc\"};
vector<string> v{\"ee\",\"ff\"};
//copy(v.begin(),v.end(), inserter(c, c.end()));//OK
//copy(v.begin(),v.end(), back_inserter(c));//NG,关联容器没有push_back
//copy(c.begin(),c.end(), inserter(v, v.end()));//OK,并且c的begin到end时拍好\\
序的
copy(c.begin(),c.end(), back_inserter(v));
for(auto &s : c){
cout << s << \" \";
}
cout << endl;
for(auto &s : v){
cout << s << \" \";
}
cout << endl;
return 0;
}
继续阅读与本文标签相同的文章
上一篇 :
Git 爆任意代码执行漏洞,所有使用者都受影响
下一篇 :
Go语言集合结构、接口说明及实现
-
Java并发系列(4)java关键字-synchronized
2026-05-18栏目: 教程
-
汇编(六)栈段、第一个汇编程序
2026-05-18栏目: 教程
-
目前Web前端就业前景如何 前端应用于哪些领域
2026-05-18栏目: 教程
-
kubernetes Service:让客户端发现pod并与之通信
2026-05-18栏目: 教程
-
新版本上线前期,产品经理要做那几点事?
2026-05-18栏目: 教程
