目录
一 关联式容器(KV模型)
二 键值对
三 树形结构的关联式容器
1 set
1.1 set的基本概念与注意事项
1.2 set的遍历
1.3 set 的查找与删除
2 multiset
3 map
3.1 概念
3.2 map的迭代器遍历方式
3.3 简化嵌套结构
3.4 统计水果出现次数
3.5 [ ]的重载使用
3.6 topK问题
4 multimap
template
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)
{}
};
set s;
s.insert(10);
s.insert(1);
s.insert(99);
s.insert(101);
s.insert(50);
s.insert(10);
s.insert(6);
s.insert(55);
s.insert(10);
//遍历方式一 迭代器
//排序加去重
set::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl;
//降序
set::reverse_iterator it1 = s.rbegin();
while (it1 != s.rend())
{
cout << *it1 << " ";
it1++;
}
cout << endl;
//遍历方法二 范围for(基于迭代器)
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
//查找
set sstr;
sstr.insert("sort");
sstr.insert("add");
sstr.insert("sum");
sstr.insert("adjust");
sstr.insert("list");
set::iterator iter = sstr.find("list");
if (iter != sstr.end())
{
cout << "找到了" << *iter << endl;
}
else
{
cout << "没找到" << endl;
}
//因为set底层为搜索二叉树KEY模型,所以不支持随便更改数据的值,否则可能会破坏结构
//删除
auto pos = sstr.find("sort");
sstr.erase(pos);
sstr.erase("list");
//map的基本使用
void map_test(void)
{
map m;
//调用pair的构造函数,构造一个匿名对象插入
m.insert(pair(1, 8.9));
m.insert(pair(5, 45.4));
m.insert(pair(7, 9.0));
m.insert(pair(2, 8.9));
m.insert(pair(3, 7.8));
m.insert(pair(7, 6.9));//插入不成功,键值相同
//调用函数模板构造对象。
//好处:不需要去声明pair的参数,让函数自己推演,用起来方便
m.insert(make_pair(7, 6.9));
//迭代器遍历
map::iterator it = m.begin();
while (it != m.end())
{
//cout << (*it).first << ":" << (*it).second << " ";
cout << it->first << ":" << it->second << " ";
it++;
}
cout << endl;
}
//简化嵌套结构
void map_test1(void)
{
typedef std::map DICT;
typedef std::pair DICT_KV;
typedef std::map::iterator DICT_IT;
//std::map dic;
DICT dic;
//dic.insert(std::pair("insert", "插入"));
//dic.insert(std::pair("sort", "排序"));
//dic.insert(std::pair("find", "查找"));
dic.insert(DICT_KV("insert", "插入"));
dic.insert(DICT_KV("sort", "排序"));
dic.insert(DICT_KV("find", "查找"));
dic.insert(DICT_KV("left", "左边"));
DICT_IT dit = dic.begin();
while (dit != dic.end())
{
//key值不可修改,但是value可以修改
dit->second.insert(0, "{");
dit->second += "}";
cout << dit->first << "--" << dit->second << endl;
dit++;
}
auto ret = dic.find("left");
if(ret != dic.end())
{
//可读性优化
string& str = ret->second;
str.insert(str.size() - 1, "、剩余");
}
cout << endl;
dit = dic.begin();
while (dit != dic.end())
{
cout << dit->first << "--" << dit->second << endl;
dit++;
}
}
//统计水果出现次数
void map_test2(void)
{
思路一
//string arr[] = { "苹果","苹果", "香蕉","西瓜", "苹果", "香蕉", "香蕉", "香蕉", "苹果", "苹果" };
//std::map countMap;
//for (const auto& str : arr)
//{
// std::map::iterator it = countMap.find(str);
// //找到了就不是第一次出现,数量加加
// if (it != countMap.end())
// {
// it->second++;
// }
// //没找到,第一次出现,插入
// else
// {
// countMap.insert(make_pair(str,1));
// }
//}
//for (const auto& e : countMap)
//{
// cout << e.first << "--" << e.second << endl;
//}
思路二
//string arr[] = { "苹果","苹果", "香蕉","西瓜", "苹果", "香蕉", "香蕉", "苹果", "苹果" };
//std::map countMap;
//for (const auto& str : arr)
//{
// auto ret = countMap.insert(make_pair(str, 1));
// if(ret.second==false)
// {
// ret.first->second++;
// }
//}
//for (const auto& e : countMap)
//{
// cout << e.first << "--" << e.second << endl;
//}
//思路三
string arr[] = { "苹果", "香蕉","西瓜", "苹果", "香蕉", "香蕉", "苹果", "苹果" };
map countMap;
for (const auto& str : arr)
{
countMap[str]++;
}
for (const auto& e : countMap)
{
cout << e.first << "--" << e.second << endl;
}
}
//[key],key为结点键值,[key]会返回结点的value的引用
void map_test3(void)
{
map sortmap;
sortmap.insert(make_pair("left", "左边"));
cout << sortmap["left"] << endl;
//插入right,value为右边。
sortmap["right"] = "右边";
//插入sort,value为缺省值
sortmap["sort"];
sortmap["insert"] = "插入";
sortmap["left"] += "、剩余";
cout << sortmap["left"] << endl;
}
//TOPK问题
struct MapItCompare
{
bool operator()(map::iterator x, map::iterator y)
{
return x->second > y->second;
}
};
void map_test4(void)
{
string arr[] = { "苹果", "香蕉","西瓜", "苹果", "香蕉", "香蕉", "苹果", "苹果" };
map countMap;
for (const auto& str : arr)
{
countMap[str]++;
}
//for (const auto& e : countMap)
//{
// cout << e.first << "--" << e.second << endl;
//}
//排序一
vector