目录
STL是C++标准库的重要组成部分,map和set容器在实际中非常的重要,关联式容器里面存储的是结构的键值对
树型关联式容器
树型结构的关联式容器主要有四种:map、set、multimap、multiset,他们使用红黑树作为其底层,容器中的元素是一个有序的序列
set和multiset容器的插入
- //set插入 排序+去重
- void test_set1()
- {
- set<int> s;
- s.insert(5);
- s.insert(6);
- s.insert(2);
- s.insert(4);
- s.insert(3);
- s.insert(2);
- s.insert(5);
-
- set<int>::iterator it = s.begin();
- while (it != s.end())
- {
- //*it = 10;不支持修改
- cout << *it << " ";//打印2 3 4 5 6 出来有序+去重
- ++it;
- }
- cout << endl;
- }
-
- //multiset 排序+不去重
- void test_set5()
- {
- multiset<int> s;
- s.insert(5);
- s.insert(6);
- s.insert(2);
- s.insert(4);
- s.insert(3);
- s.insert(2);
- s.insert(5);
-
- set<int>::iterator it = s.begin();
- while (it != s.end())
- {
- //*it = 10;不支持修改
- cout << *it << " ";//打印2 2 3 4 5 5 6 出来有序+不去重
- ++it;
- }
- cout << endl;
-
- cout << s.count(5) << endl;//5出现的次数
- cout << s.erase(5) << endl;//删除所有5 返回删除了几个
-
- for (auto e : s)
- {
- cout << e << " ";//打印2 2 3 4 6
- }
- cout << endl;
- }
set容器的查找
- //find 返回迭代器 不能修改
- void test_set2()
- {
- set<int> s;
- s.insert(5);
- s.insert(6);
- s.insert(2);
- s.insert(4);
-
- set<int>::iterator pos = s.find(2);//0(logN)
-
- if (pos != s.end())//set本身的find
- {
- cout << "set.find找到了" << endl;
- }
-
- pos = find(s.begin(), s.end(), 2);//0(N)
- if (pos != s.end())//算法当中的find
- {
- cout << "find找到了" << endl;
- }
-
- }
set容器的删除
- //删除数据
- void test_set3()
- {
- set<int> s;
- s.insert(5);
- s.insert(6);
- s.insert(2);
- s.insert(4);
- s.insert(6);
- s.insert(9);
-
- int x;
- while (cin >> x)//持续输入
- {
- set<int>::iterator pos = s.find(x);
- if (pos != s.end())
- {
- s.erase(pos);
- cout << "删除" << x << "成功" << endl;
- }
- else
- {
- cout << x << "不在set中" << endl;
- }
-
- for (auto e : s)
- {
- cout << e << " ";
- }
- cout << endl;
- }
- }
lower_bound和upper_bound
- //lower_bound 返回>=位置的迭代器
- //upper_bound 返回>位置的迭代器
- void test_set4()
- {
- set<int> s;
- s.insert(5);
- s.insert(6);
- s.insert(2);
- s.insert(4);
- s.insert(9);
-
- for (auto e : s)
- {
- cout << e << " ";
- }
- cout << endl;
-
- //删除x<=[]<=y的区间
- int x, y;
- cin >> x >> y;
- auto left = s.lower_bound(x);
- auto right = s.upper_bound(y);//
- s.erase(left,right);
-
- for (auto e : s)
- {
- cout << e << " ";
- }
- cout << endl;
- }
键值对一般只包含两个成员变量key和value,key代表键值,value表示与key对应的信息,比如词典中学生(key)对应student(value)
SGI-STL中关于键值对的定义:
- 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)
- {}
- };
map容器的初始化
- void test_map1()
- {
- map
dict; - //调用pair 构造函数
- dict.insert(pair
("sort", "排序")); -
- //构造函数
- pair
kv("insert", "插入"); - dict.insert(kv);
-
- //函数模板
- dict.insert(make_pair("left", "左边"));
- dict.insert(make_pair("left", "剩余"));
- }
map容器的遍历
- //用map存一个小字典
- void test_map1()
- {
- map
dict; - //调用pair 构造函数
- dict.insert(pair
("sort", "排序")); - //构造函数
- pair
kv("insert", "插入"); - dict.insert(kv);
- //函数模板
- dict.insert(make_pair("left", "左边"));
- dict.insert(make_pair("left", "剩余"));
-
- //迭代器遍历遍历
- //map
::iterator it = dict.begin(); - auto it = dict.begin();
- while (it != dict.end())
- {
- //pair (将key和value打包)
- cout << it->first << ":" << it->second << endl;
- ++it;
- }
- cout << endl;
-
- //auto 遍历
- for (auto& kv : dict)
- {
- cout << kv.first << ":" << kv.second << endl;
- }
- }
map容器的查找
- //map支持[]操作符,在operator[]中实际进行插入查找
- //统计下面每个词语的次数
- void test_map2()
- {
- string arr[]{ "大学","中庸","论语","孟子","孔子"
- ,"大学","史记","论语" };
-
- map
int>countMap; - for (auto& str : arr)
- {
- countMap[str]++;//统计次数 []返回的是value的引用
- //查找+修改 插入+修改
- }
- for (auto& kv : countMap)
- {
- cout << kv.first << ":" << kv.second << endl;
- }
- }
operator[]的原理是:
用构造一个键值对,然后调用insert()函数将该键值对插入到map中
如果key已经存在,插入失败,insert函数返回该key所在位置的迭代器
如果key不存在,插入成功,insert函数返回新插入元素所在位置的迭代器
operator[]函数最后将insert返回值键值对中的value返回
学习set和map一定要学会查看文档,这里放一个官方文档链接map文档和set文档,set和map在实际中非常的重要,我们只要熟悉常见的接口就可以
希望这篇文章大家有所收获,我们下篇见