【知识预告】
我们已经接触过STL中的部分容器,比如:vector、list、deque、forward_list(C++11)等,这些容器统称为序列式容器,因为其底层为线性序列的数据结构,里面存储的是元素本身。
关联式容器也是用来存储数据的,与序列式容器不同的是,其里面存储的是
用来表示具有一一对应关系的一种结构,该结构中一般只包含两个成员变量key和value,key代表键值,value表示与key对应的信息。比如:现在要建立一个英汉互译的字典,那该字典中必然有英文单词与其对应的中文含义,而且,英文单词与其中文含义是一一对应的关系,即通过该应该单词,在词典中就可以找到与其对应的中文含义。
根据应用场景的不同,STL总共实现了两种不同结构的管理式容器:树型结构与哈希结构。树型结构的关联式容器主要有四种:map、set、multimap、multiset。这四种容器的共同点是:使用平衡搜索树(即红黑树)作为其底层结果,容器中的元素是一个有序的序列。下面一依次介绍每一个容器。
set文档介绍
注意:
直接看代码,简单易懂(有注释)。
int main()
{
set<int> s;
s.insert(5);
s.insert(2);
s.insert(6);
s.insert(1);
s.insert(1);
s.insert(2);
s.insert(5);
s.insert(5);
//auto it = s.begin();
set<int>::iterator it = s.begin(); // 支持迭代器
while (it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl; // 1 2 5 6 排序 + 去重
for (auto e : s)
{
cout << e << " ";
}
cout << endl; // 1 2 5 6 支持范围for
// 删除存在的
s.erase(2);
// 删除不存在的
//set::iterator it = s.find(3);
it = s.find(30);
if (it != s.end())
{
s.erase(it);
}
for (auto e : s)
{
cout << e << " ";
}
cout << endl; // 1 5 6
if (s.count(3)) // 查找3在不在
{
cout << "3在" << endl;
}
else
{
cout << "3不在" << endl; // 3不在
}
return 0;
}
int main()
{
set<int> myset;
set<int>::iterator itlow, itup;
for (int i = 1; i < 10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90
//itlow = myset.lower_bound(30);
itlow = myset.lower_bound(25); // 返回的是 >= val的iterator
itup = myset.upper_bound(60); // 返回的是> val的iterator
myset.erase(itlow, itup); // 10 20 70 80 90
cout << "myset contains:";
for (set<int>::iterator it = myset.begin(); it != myset.end(); ++it)
{
cout << ' ' << *it;
}
cout << '\n';
// myset contains: 10 20 70 80 90
return 0;
}
multiset文档介绍
int main()
{
multiset<int> s;
s.insert(5);
s.insert(2);
s.insert(6);
s.insert(1);
s.insert(1);
s.insert(2);
s.insert(1);
s.insert(5);
s.insert(2);
multiset<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl; // 1 1 1 2 2 2 5 5 6
for (auto e : s)
{
cout << e << " ";
}
cout << endl; // 1 1 1 2 2 2 5 5 6
// 如果有多个值,find返回中序第一个val
it = s.find(2);
while (it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl; // 2 2 2 5 5 6
// 查看有几个1
cout << s.count(1) << endl; // 3
// [>=val, >val)
//auto ret = s.equal_range(2);
//s.erase(ret.first, ret.second);
size_t n = s.erase(2); // 删除全部的2
cout << n << endl; // 3
for (auto e : s)
{
cout << e << " ";
}
cout << endl; // 1 1 1 5 5 6
return 0;
}
map的文档简介
int main()
{
map<string, string> dict;
dict.insert(pair<string, string>("sort", "排序"));
dict.insert(pair<string, string>("insert", "插入"));
dict.insert(pair<const char*, const char*>("left", "左边"));
// make_pair可以自动识别类型
dict.insert(make_pair("right", "右边")); // 以后推荐这种写法
// erase如果不存在,那就插入erase,并且使用默认val(一般是0)
dict["erase"];
cout << dict["erase"] << endl;
// erase如果存在,那就是查找,返回的是erase的val(引用返回)
dict["erase"] = "删除"; // 修改erase的val
cout << dict["erase"] << endl;
dict["test"] = "测试";
dict["left"] = "左边、剩余";
// 这里的方括号操作符,很神奇,功能很多
string s1("xxx"), s2("yyy");
dict.insert(make_pair(s1, s2));
map<string, string>::iterator it = dict.begin();
while (it != dict.end())
{
cout << (*it).first << ":";
cout << (*it).second << endl;
//cout << it->second << endl;
it++;
}
return 0;
}
int main()
{
map<string, string> dict;
dict.insert(pair<string, string>("sort", "排序"));
dict.insert(pair<string, string>("insert", "插入"));
// make_pair可以自动识别类型
dict.insert(make_pair("right", "右边"));
dict["test"] = "测试";
dict["left"] = "左边、剩余";
string s1("xxx"), s2("yyy");
dict.insert(make_pair(s1, s2));
for (const auto& kv : dict)
{
cout << kv.first << ":" << kv.second << endl;
}
for (auto& kv : dict)
{
//kv.first += 'x'; // 报错
kv.second += 'x'; // 不报错
// first不可以修改,second可以修改
cout << kv.first << ":" << kv.second << endl;
}
return 0;
}
int main()
{
// 统计水果出现的次数
string arr[] = { "苹果","西瓜","苹果","西瓜","苹果",
"苹果","西瓜","苹果","香蕉","苹果","香蕉" };
map<string, int> countMap;
for (auto& str : arr)
{
auto ret = countMap.find(str);
if (ret == countMap.end())
{
// 没有表示第一次出现,插入
countMap.insert(make_pair(str, 1));
}
else
{
// 次数++
ret->second++;
// (*ret).second++;
}
}
for (auto& kv : countMap)
{
cout << kv.first << ":" << kv.second << endl;
}
// 苹果:6
// 西瓜:3
// 香蕉:2
return 0;
}
int main()
{
string arr[] = { "苹果","西瓜","苹果","西瓜","苹果",
"苹果","西瓜","苹果","香蕉","苹果","香蕉" };
map<string, int> countMap;
for (auto& str : arr)
{
countMap[str]++;
// 方括号,给它一个key,它返回对应key的val(val的引用)
}
for (auto& kv : countMap)
{
cout << kv.first << ":" << kv.second << endl;
}
// 苹果:6
// 西瓜:3
// 香蕉:2
return 0;
}
multimap文档介绍
注意:multimap和map的唯一不同就是:map中的key是唯一的,而multimap中key是可以重复的。
int main()
{
multimap<string, string> dict;
dict.insert(make_pair("left", "左边"));
dict.insert(make_pair("left", "剩余"));
dict.insert(make_pair("left", "左边"));
for (auto& kv : dict)
{
cout << kv.first << ":" << kv.second << endl;
}
// left:左边
// left:剩余
// left:左边
return 0;
}
例题:两个数组的交集
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
{
// 排序 + 去重
set<int> s1(nums1.begin(),nums1.end());
set<int> s2(nums2.begin(),nums2.end());
// set排过序,依次比较,小的一定不是交集,相等的是交集
auto it1 = s1.begin();
auto it2 = s2.begin();
vector<int> ret;
while(it1 != s1.end() && it2 != s2.end())
{
if(*it1 < *it2)
{
it1++;
}
else if(*it2 < *it1)
{
it2++;
}
else
{
ret.push_back(*it1);
it1++;
it2++;
}
}
return ret;
}
};