1.序列式容器和关联式容器
C++STL中的部分容器如:list,vector,string,deque,这些容器统称序列式容器,因为逻辑结构为线性的数据结构,两个位置储存的值之间一般,没有紧密的关联关系,交换一下,他依旧是序列式容器。顺序容器的元素是按他们在容器中的储存位置来顺序保存和访问的。
关联式容器也是用来储存数据结构的,与序列式容器不同的是,关联式容器逻辑结构是非线性结构,两个位置有紧密的关联关系,交换一下他的数据结构就被破坏了。关联式容器有map/set系列和unordered_map/unordered_set系列。
map和set底层是红⿊树,红⿊树是⼀颗平衡⼆叉搜索树。set是key搜索场景的结构, map是key/value搜索场景的结构。
2.set的使用
• set的声明如下,T就是set底层关键字的类型
• set默认要求T⽀持⼩于⽐较,如果不⽀持或者想按⾃⼰的需求⾛可以⾃⾏实现仿函数传给第⼆个模 版参数
• set底层存储数据的内存是从空间配置器申请的,如果需要可以⾃⼰实现内存池,传给第三个参 数。
• ⼀般情况下,我们都不需要传后两个模版参数。
• set底层是⽤红⿊树实现,增删查效率是 O(logN)的,迭代器遍历是⾛的搜索树的中序,所以是有序。
template < class T,
class Compare = less<T>,
// set::key_type/value_type
class Alloc = allocator<T>
> class set;
2.1set的构造和迭代器
set的⽀持正向和反向迭代遍历,遍历默认按升序顺序,因为底层是⼆叉搜索树,迭代器遍历⾛的中 序;⽀持迭代器就意味着⽀持范围for,set的iterator和const_iterator都不⽀持迭代器修改数据,修改关键字数据,破坏了底层搜索树的结构。
2.2set的增删查
#include<iostream>
#include<set>
using namespace std;
int main()
{
// 去重+升序排序
set<int> s;
// 去重+降序排序(给⼀个⼤于的仿函数)
//set<int, greater<int>> s;
s.insert(5);
s.insert(2);
s.insert(7);
s.insert(5);
//set<int>::iterator it = s.begin();
auto it = s.begin();
while (it != s.end())
{ // error C3892: “it”: 不能给常量赋值
// *it = 1;
cout << *it << " ";
++it;
}
cout << endl;
// 插⼊⼀段initializer_list列表值,已经存在的值插⼊失败
s.insert({ 2,8,3,9 });
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
set<string> strset = { "sort", "insert", "add" };
// 遍历string⽐较ascll码⼤⼩顺序遍历的
for (auto& e : strset)
{
cout << e << " ";
}
cout << endl;
}
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int> s = {7,4,3,8,2,1,6,5};
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
s.erase(s.begin());//删除最小值
int x ;//直接删除
cin >>x;
if (s.erase(x))
{
cout << "删除成功" << endl;
}
else
cout << x << "不存在" << endl;
cin >> x;
auto pos = s.find(x);//利用查找再用迭代器删除
if (pos != s.end())//如果查找不到则返回s.end
s.erase(pos);
else
cout << x << "不存在" << endl;
std::set<int> myset;
for (int i = 1; i < 10; i++)
myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90
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;
return 0;
}
2.3multiset和set的差异 multiset和set的使⽤基本完全类似,主要区别点在于multiset⽀持值冗余。与set不同的是erase(x)时会啥删除所有x值的节点,find会查找中序中的第一个x值的节点,count会返回x值的个数。
3.1mat类的介绍
map的声明如下,Key就是map底层关键字的类型,T是map底层value的类型,set默认要求Key⽀持⼩于⽐较,如果不⽀持或者需要的话可以⾃⾏实现仿函数传给第⼆个模版参数,map底层存储数据的 内存是从空间配置器申请的。⼀般情况下,我们都不需要传后两个模版参数。map底层是⽤红⿊树实现,增删查改效率是 O(logN),迭代器遍历是⾛的中序,所以是按key有序顺序遍历的。map底层的红⿊树节点中的数据,使⽤pair存储键值对数据。
template < class Key,
class T,
class Compare = less<Key>,
class Alloc = allocator<pair<const Key,T> >
> class map;
3.2map的一些常规使用
#include<iostream>
#include<map>
using namespace std;
int main()
{
//使用 initializer_list构造及迭代遍历
map<string,string> m= { {"left", "左边"}, {"right", "右边"},{"insert", "插⼊"},{ "string", "字符串" } };
auto it = m.begin();
while (it != m.end())
{
cout << it->first << " " << it->second;
it++;
}
cout << endl;
// insert插⼊pair对象的4种⽅式,对⽐之下,最后⼀种最⽅便
pair<string, string> kvl("first", "第一个");
m.insert(kvl);
m.insert(pair<string, string>("second", "第二个"));
m.insert(make_pair("sort", "排序"));
m.insert({ "auto","自动" });
m.insert({ "left","左边,剩余" });//"left"已经存在,插⼊失败,但是会修改val值
// 范围for遍历
for (const auto& e : m)
{
cout << e.first << ":" << e.second << endl;
}
cout << endl;
string str;
while (cin >> str)
{
auto ret = m.find(str);
if (ret != m.end())
{
cout << "->" << ret->second << endl;
}
else
{
cout << "⽆此单词,请重新输⼊" << endl;
}
}
// erase等接⼝跟set完全类似,这⾥就不演⽰讲解了
return 0;
}