创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。
提示:以下是本篇文章正文内容,下面案例可供参考。
一、关联式容器
在初阶阶段,我们已经接触过
STL
中的部分容器,比如:
vector
、
list
、
deque
、forward_list(C++11)等,这些容器统称为序列式容器,因为其底层为线性序列的数据结构,里面
存储的是元素本身。那什么是关联式容器?它与序列式容器有什么区别?
关联式容器
也是用来存储数据的,与序列式容器不同的是,其
里面存储的是
<key, value>
结构的
键值对,在数据检索时比序列式容器效率更高。
二、set
1.创建和遍历
#include<iostream>
#include<set>
using namespace std;
int main()
{
// 排序 + 去重
set<int> s;
s.insert(8);
s.insert(1);
s.insert(6);
s.insert(5);
s.insert(8);
s.insert(7);
s.insert(6);
set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
it++;
}
return 0;
}
2.删除
//第一种:直接删除
s.erase(6);
//第二种:找到元素所在位置删除、
set<int>::iterator itt = s.find(8);
if (itt != s.end())
s.erase(itt);
3.lower_bound和upper_bound
#include<iostream>
#include<set>
using namespace std;
int main()
{
std::set<int> s;
std::set<int>::iterator itlow, itup;
for (int i = 1; i < 10; i++)
s.insert(i * 10); // 10 20 30 40 50 60 70 80 90
itlow = s.lower_bound(25); // >= val值位置的iterator
itup = s.upper_bound(70); // > val值位置的iterator
// [25, 70]
s.erase(itlow, itup);
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
return 0;
}
4.multiset
#include<iostream>
#include<set>
using namespace std;
int main()
{
// 排序
multiset<int> s;
s.insert(8);
s.insert(1);
s.insert(6);
s.insert(5);
s.insert(8);
s.insert(7);
s.insert(6);
set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
it++;
}
return 0;
}
5.find
#include<iostream>
#include<set>
using namespace std;
int main()
{
// 排序
multiset<int> s;
s.insert(8);
s.insert(1);
s.insert(6);
s.insert(5);
s.insert(8);
s.insert(7);
s.insert(6);
set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
it++;
}
cout << endl;
// 如果有多个值,find返回中序第一个val
set<int>::iterator itt = s.find(6);
while (itt != s.end())
{
cout << *itt << " ";
++itt;
}
return 0;
}
三、map
1.创建和遍历
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string, string> dict;
dict.insert(pair<string, string>("sort", "排序"));
dict.insert(pair<string, string>("insert", "插入"));
//推荐使用make_pair
dict.insert(make_pair("left", "左"));
string s1("aa"), s2("bb");
dict.insert(make_pair(s1, s2));
map<string, string>::iterator it = dict.begin();
while (it != dict.end())
{
cout << (*it).first << " : " << (*it).second << endl;
cout << it.operator->()->first << " : " << it.operator->()->second << endl;
//it.operator->()->相当于it->
cout << it->first << " : " << it->second << endl;
it++;
}
for (auto& kv : dict)
{
// kv.first += 'x'; first不支持修改
kv.second += 'x';
cout << kv.first << ":" << kv.second << endl;
}
return 0;
}
2.统计出现次数
①方法一
#include<iostream>
#include<map>
using namespace std;
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++;
}
for (auto& kv : countmap)
cout << kv.first << ":" << kv.second << endl;
return 0;
}
①方法二:[ ] 的使用
#include<iostream>
#include<map>
using namespace std;
int main()
{
string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
map<string, int> countmap;
for (auto& str : arr)
countmap[str]++;
for (auto& kv : countmap)
{
cout << kv.first << ":" << kv.second << endl;
}
return 0;
}
operator[ ]是用insert实现的
insert的返回值:
①如果插入成功,返回新插入Key所在的节点的迭代器和true;充当查找功能
②如果插入失败,返回已经存在的Key所在节点的迭代器和false;充当插入和查找功能
[ ]的函数可以简写为如下:
函数返回值类型是Value&,调用参数是key;先调用insert,无论成功还是失败,都会返回迭代器;取迭代器ret.first的value:ret.first->second.
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string, string> dict;
dict.insert(pair<string, string>("sort", "排序"));
dict.insert(pair<string, string>("insert", "插入"));
//推荐使用make_pair
dict.insert(make_pair("left", "左"));
dict["erase"]; // 插入
cout << dict["erase"] << endl; // 查找
dict["erase"] = "删除"; // 修改
cout << dict["erase"] << endl;// 查找
dict["test"] = "测试"; // 插入+修改
dict["left"] = "左边、剩余"; // 修改
return 0;
}
总结
例如:以上就是今天要讲的内容,本文仅仅简单介绍了c++的基础知识。