一、序列是容器和关联式容器
前面我们接触过部分STL容器如:string、vector、list、deque、array等,这些容器统称为序列式容器,因为逻辑结构为线性序列的数据结构,两个位置存储的值一般没有紧密的关联关系,比如交换一下,他依旧是序列式容器。顺序容器中的元素是按他们在容器中存储的位置来顺序保存和访问的。
关联式容器也是用来存储数据的,与序列式容器不同的是,关联式容器的逻辑结构通常是非线性结构,两个位置有紧密关联关系,交换一下,他的存储结构就被破坏了,顺序容器中得到元素是按关键字来保存和访问的。关联式容器有map/set系列和unordered_map和unordered_set系列。
本文讲的是map和set,其底层是红黑树,红黑树是一颗平衡二叉搜索树。set是key搜索场景的结构,map是key/value搜索场景的结构。
二、set系列的使用
1.set和multiset参考文档
https://legacy.cplusplus.com/reference/set/
2.set类介绍

如图 T为set底层关键字的类型
其默认T支持小于比较,如果想按照自己的想法走,可以自行实现仿函数传给第二个模版参数
set的底层存储数据的内存是从空间配置器申请的,如果需要,可以自己实现内存池,传给第三个参数。
一般情况下,我们都不需要传后两个模版参数
set的底层是用红黑树实现,增改查的效率是O(logN),迭代器走中序遍历,所以是有序的。
3.set的构造和迭代器
set的构造我们关注如下几个接口即可
set的支持正向反向迭代遍历,遍历默认升序排序,因为底层是二叉搜索树,迭代器走的是中序遍历;支持迭代器也就意味着支持范围for,set的iterator和const_iterator都不支持迭代器修改数据,修改关键字数据会破坏底层搜索树的结构。
cpp
//无参默认构造
explicit set (const key_compare& comp=key_compare(),const allocator_type& alloc=allocator_type())
//迭代器区间构造
template<class InputIterator>
set (InputIterator first,InputIterator last,
const key_compare& comp=key_compare(),
const allocator_type& alloc=allocator_type();)
//拷贝构造
set(const set& x);
//initializer 列表构造
set (initializer_list<value_type> il,
const key_compare& comp=key_compare(),
const allocator_type& alloc=allocator_type();)
//迭代器是一个双向迭代器
iterator ->a bidirectional iterator to const value_type
//正向迭代器
iterator begin()
iterator end()
//反向迭代器
reverse_iterator rbegin()
reverse_iterator rend()
4.set的增删查
set的增删查关注以下几个接口即可:
cpp
Member types
key_type -> The first template parameter(T)
value->type ->The first template parameter(T)
//单个数据插入,如果已经存在则插入失败
pair<iterator,bool> insert(const value_type& val)
//列表插入,已经在容器中存在的值不会插入
void insert(initializer_list<value_type> il);
//迭代器区间插入,已经在容器中存在的值不会插入。
template<class InputIterator>
void insert(InputIterator first,InputIterator last);
//查找val,返回val所在迭代器,没有找到返回end()
iterator find(const value_type& val);
//查找val 返回val的个数
size_type conut(const value_type& val) const;
//删除一个迭代器位置的值
iterator erase(const_iterator position);
//删除val 不存在返回0,存在返回1
size_type erase(const value_type& val);
//删除一段迭代器区间的值
iterator erase(const_iterator first,const_iterator last);
//返回大于等于val位置的迭代器,如果都小于,返回end()
iterator lower_bound(const value_type& val) const;
//返回大于val位置的迭代器
iterator upper_bound(const value_type& val) const;
5.insert和迭代器遍历的使用样例
cpp
#include<iostream>
#include<set>
#include<map>
using namespace std;
void test01() {
set<int> s;
int arr[] = { 1,9,5,3,3,4,6,2,7, };
for (auto a : arr) {
s.insert(a);
}
auto i = s.begin();
while (i != s.end()) {
cout << *i << " ";
i++;
}
cout << endl;
s.insert({ 1,8 });
for (auto e : s) {
cout << e << " ";
}
cout << endl;
}
void test02() {
cout << "///////////////////////////////" << endl;
set<string> strset = { "abandon","banana","cat","big" };
for (auto& w : strset) {
cout << w << " ";
}
cout << endl;
}
int main() {
test01();
test02();
}
6.find和erase使用样例
cpp
void test03() {
set<int> s = { 4,2,7,2,8,5,9 };
for (auto e : s) {
cout << e << " ";
}
cout << endl;
//删除最小值
s.erase(s.begin());
for (auto e : s) {
cout << e << " ";
}
cout << endl;
//删除特定的值
s.erase(5);
for (auto e : s) {
cout << e << " ";
}
cout << endl;
//删除一段区间
s.erase(s.begin(), s.end());
for (auto e : s) {
cout << e << " ";
}
}
int main() {
test03();
}
7.lower_bound和upper_bound
cpp
void test04() {
set<int> myset;
for (size_t i = 1; i < 10; i++) {
myset.insert(i * 10);
}
for (auto e : myset) {
cout << e << " ";
}
cout << endl;
//实现查找到[itlow,itup)包含[30,60]区间
//返回>=30
auto itlow = myset.lower_bound(30);
//返回>60
auto itup = myset.upper_bound(60);
myset.erase(itlow, itup);
for (auto e : myset) {
cout << e << " ";
}
cout << endl;
}
int main() {
test04();
}
8.multiset和set的差异
multiset和set的使用基本完全类似,主要区别点在于multiset支持值的冗余,那么insert/find/count/erase都围绕着支持值冗余有所差异。
cpp
void test05() {
//与set不同的是,multiset是排序但不去重
multiset<int> s = { 1,3,5,5,7,9,5,9,4,2,6,8 };
auto it = s.begin();
while (it != s.end()) {
cout << *it << " ";
++it;
}
cout << endl;
int x = 5;
auto pos = s.find(x);
//相比set不同的是,find会返回中序第一个的迭代器
while (pos != s.end() && *pos == x) {
cout << *pos << " ";
++pos;
}
cout << endl;
//相比set不同的是,cout会返回x的实际个数
cout << s.count(x) << endl;
//相比set不同的是,erase会删除所有的x
s.erase(x);
for (auto e : s) {
cout << e << " ";
}
cout << endl;
}
int main() {
test05();
}
三、map系列的使用
1.map和multimap的参考文档
https://legacy.cplusplus.com/reference/map/
2.map类的介绍
map的声明如下。Key就是map底层关键字的类型,T是map底层value的类型,set默认要求Key支持小于比较,如果有需要,也可自行实现仿函数传给第二个模版参数,map底层存数据的内存是从空间配置器申请的。一般情况下,我们都不需要传后两个模版参数。map底层是用红黑树实现的,增删查改的效率是O(logN),迭代器遍历走的中序遍历,所以是按Key有序顺序遍历的。
cpp
template<class Key,
class T,
class Compare=less<Key>
class Alloc=allocator<pair<const Key,T>>
>class map
3.pair类型介绍
map底层的红黑树节点中的数据,使用pair<Key,T>存储键值对数据
cpp
typedef pair<const Key,T> value_type;
template<class T1,class T2>
struct pair{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair():first(T1()),second(T2())
{}
pair(const T1&a,const T2& b):first(a),second(b)
{}
template<class U,class V>
pair(const pair<U,V>& pr):first(pr.first),second(pr.second)
{}
};
template<class T1,class T2>
inline pair<T1,T2> make_pair(T1 x,T2 y){
return (pair<T1,T2> (x,y)
}
4.map的构造
map的构造我们关注以下几个接口即可。
map的支持正向和反向迭代遍历,遍历默认按key的升序顺序。因为底层是二叉搜索树,迭代器遍历走的中序,支持迭代器就意味着支持范围for,map支持修改value数据,不支持修改key的数据,修改关键字数据,破坏底层搜索树的结构。
cpp
//无参默认构造
explicit map (const key_compare& comp=key_compare()
const allocator_type& alloc =allocator_type());
//迭代器区间构造
template<class InputIterator>
map(InputInterator first,InputIterator last,
const key_compare& comp=key_compare()
const allocator_type& alloc =allocator_type());
//拷贝构造
map(const map& x);
//initializer 列表构造
map (initializer_list<value_type> il,
const key_compare& comp=key_compare()
const allocator_type& alloc =allocator_type());
//迭代器是一个双向迭代器
iterator -> a bidirectional iterator to const value_type
//正向迭代器
iterator begin()
iterator end()
//反向迭代器
reverse_iterator rbegin()
reverse_iterator rend()
5.map的增删查
map的增删查关注以下几个接口即可;
map增接口,插入的pair键值对数据,跟set有所不同,但是查和删的接口只用关键字key跟set是完全类似的,不过find返回iterator,不仅仅可以确认key在不在,还找到key映射的value,同时通过迭代器还可以修改value
cpp
Member types
key_type ->The first template parameter (Key)
mapped_type -> The second template parameter(T)
value_type ->pair<const key_type,mapped_type>
//单个数据插入,如果已经key存在则插入失败,key存在相等value不相等也会插入失败
pair<iterator,bool> insert(const value_type& val);
//列表插入,已经在容器中存在的值不会插入
void insert(initializer_list<value_type> il);
//迭代器区间插入,已经在容器中存在的值不会插入
template<class InputIterator>
void insert(InputIterator first,InputIterator last)
//查找k,返回k所在的迭代器,没有找到返回end()
iterator find(const key_type& k);
//查找k,返回k的个数
size_type count(const key_type& k) const;
//删除一个迭代器位置的值
iterator erase(const_iterator postition);
//删除k,k不存在返回0,存在返回1
size_type erase(const key_type& k)
//删除一段迭代器区间的值
iterator erase(const_iterator first,const_iterator last);
//返回大于等于k位置的迭代器
iterator lower_bound (const key_type& k);
//返回大于k位置的迭代器
iterator upper_bound(const key_type& k);
6.map的数据修改
前面我们提到了map支持修改mapped_type数据,不支持修改key数据,修改关键字数据,破坏了底层搜索树的结构。
map第一个支持修改的方式时通过迭代器,迭代器遍历是或者find返回key所在的iterator修改。map还有一个非常重要的修改接口operator[],但是operator[]不仅仅支持修改,还支持插入数据和查找数据,所以他是一个多功能复合接口。
需要注意从内部实现角度,map这里把我们传说的value值,给的是T类型,typedef为mapped_type.而value_type是红黑树结点中存储的pair键值对值。日常使用我们还是喜欢将这里T的映射值叫做value。
cpp
mapped_type& operator[](const key_type& k){
//1.如果k不在map中,insert会插入k和mapped_type默认值,同时[]返回结点中存储mapped_type值的引用,那么我们就可以通过引用来修改映射值。所以[]具备了插入+修改功能
//2.如果k在map中,insert会插入失败,但是insert返回pair对象的first是指向key结点的迭代器,返回值同时[]返回节点中存储的mapped_type值的引用,所以[]具备了查找加修改的功能。
pair<iterator,bool> ret=insert({k,mapped_type()});
iterator it =ret.first;
return it.second;
}
7.构造遍历及增删查使用样例
cpp
void test06() {
//initializer_list 构造及迭代遍历
map<string, string> dict = { {"left","左边"},
{"abandan","放弃"},{"insert","插入"} };
auto it = dict.begin();
while (it != dict.end()) {
cout << it->first << ":" << it->second<<endl;
it++;
}
cout << endl;
//insert的四中插入pair对象的方式
pair<string, string> kv1("first", "第一个");
dict.insert(kv1);//直接插入
dict.insert(pair<string, string>("second", "第二个"));//匿名对象
dict.insert(make_pair("third", "第三个"));//调用模版函数
dict.insert({ "forth","第四个" });//列表初始化
//left存在插入失败
dict.insert({ "left","左边的" });
for (auto& e : dict) {
cout << e.first << ":" << e.second << endl;
}
cout << endl;
string str;
while (cin >> str) {
auto ret = dict.find(str);
if (ret != dict.end()) {
cout << "->" << ret->second << endl;
}
else {
cout << "无此单词,请重新输入" << endl;
}
}
}
int main() {
test06);
}
8.Map的迭代器和[]功能样例
cpp
void test07() {
string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜",
"苹果", "香蕉", "苹果", "香蕉" };
map<string, int> countMap;
for (const auto& str : arr) {
auto ret = countMap.find(str);
if (ret==countMap.end()) {
countMap.insert({str, 1});
}
else {
ret->second++;
}
}
for (auto& e : countMap) {
cout << e.first << ":" << e.second << endl;
}
}
void test08(){
string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜",
"苹果", "香蕉", "苹果", "香蕉" };
map<string, int> countMap;
for (const auto& str : arr) {
countMap[str]++;
}
for (auto& str:countMap) {
cout << str.first << "->" << str.second;
}
//key不存在,插入{"insert",0}
countMap["insert"];
//插入+修改
countMap["橘子"] = 9;
//修改
countMap["西瓜"] = 10;
//key存在->查找
cout << countMap["香蕉"] << endl;
for (auto& str : countMap) {
cout << str.first << "->" << str.second;
}
}
int main() {
test07();
test08();
}
9.multimap和map的差异
multimap和map的使用基本完全类似,主要区别点在于multimap支持关键值key冗余,那么insert/find/count/erase都围绕着支持关键字key冗余有所差异,这里和set/multiset完全一样,比如find时,有多个key,返回中序第一个。其次就是multimap不支持[],因为支持key冗余,[]就只能支持插入了,不能支持修改。