之前的学习之中 , 我们已经接触过STL中的部分容器,比如:vector、list、deque、forward_list(C++11)等,这些容器统称为序列式容器,因为其底层为线性序列的数据结构,里面存储的是元素本身。
而map与set是关联性容器 , 那什么是关联式容器?它与序列式容器有什么区别?
关联式容器也是用来存储数据的,与序列式容器不同的是,其里面存储的是<key, value>结构的键值对,在数据检索时比序列式容器效率更高!
⚠️键值对是一个重要的概念!!!
键值对用来表示具有一一对应关系的一种结构,该结构中一般只包含两个成员变量key和value,key代表键值,value表示与key对应的信息。
比如:现在要建立一个英汉互译的字典,那该字典中必然有英文单词与其对应的中文含义,而且,英文单词与其中文含义是一一对应的关系,即通过该应该单词,在词典中就可以找到与其对应的中文含义。
就是类似映射关系,通过键值key可以找到对应的value!!!
根据应用场景的不同,STL总共实现了两种不同结构的管理式容器:树型结构与哈希结构。树型结构的关联式容器主要有四种:map set multimap multiset。这四种容器的共同点是:使用平衡搜索树(即红黑树)作为其底层结果,容器中的元素是一个有序的序列。
Set Sets are containers that store uniqueelementsfollowing a specific order.
In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.
Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
set containers are generally slower than unordered_set containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.
Sets are typically implemented as binary search trees.
总结:
value就是key,类型为T),并且每个value必须是唯一(unique)的。set中的元素不能在容器中修改(元素总是const),但是可以从容器中插入或删除它们。set中的元素总是按照其内部比较对象(类型比较)所指示的特定严格弱排序准则进行排序。set容器通过key访问单个元素的速度通常比unordered_set容器慢,但它们允许根据顺序对子集进行直接迭代。set在底层是用二叉搜索树(红黑树)实现的注意
map/multimap不同,map/multimap中存储的是真正的键值对<key, value>,set中只放value,但在底层实际存放的是由<value, value>构成的键值对。set中插入元素时,只需要插入value即可,不需要构造键值对。set中的元素不可以重复(因此可以使用set进行去重)。set的迭代器遍历set中的元素,可以得到有序序列set中的元素默认按照小于来比较set中查找某个元素,时间复杂度为:set中的元素不允许修改(为什么?)set中的底层使用二叉搜索树(红黑树)来实现先来看其模版参数:

我们一般需要提供T键值,Compare仿函数默认是按升序排,有需求可以自行传入!!!Alloc是set中元素空间的管理方式,使用STL提供的空间配置器管理。
有了这个我们就可以实例化需要的set容器,然后使用构造函数来构造:
函数声明 功能介绍
函数声明 | 功能介绍 |
|---|---|
set (const Compare& comp = Compare(), const Allocator&= Allocator() ); | 构造空的set |
set (InputIterator first, InputIterator last, const Compare& comp = Compare(), const Allocator& =Allocator() ); | 用[first, last)区间中的元素构造set |
set ( const set<Key,Compare,Allocator>& x); | set的拷贝构造 |
与我们之前学习的容器大差不差!!!
set通过的接口与之前学习的容器有所不同
函数声明 | 功能介绍 |
|---|---|
pair<iterator,bool> insert (const value_type& x ) | 在set中插入元素x,实际插入的是<x, x>构成的键值对,如果插入成功,返回<该元素在set中的位置,true>, 如果插入失败,说明x在set中已经存在,返回<x在set中的位置,false> |
void erase ( iterator position ) | 删除set中position位置上的元素 |
size_type erase ( constkey_type& x ) | 删除set中值为x的元素,返回删除的元素的个数 |
void erase ( iterator first,iterator last ) | 删除set中[first, last)区间中的元素 |
void swap (set<Key,Compare,Allocator>&st ); | 交换set中的元素 |
void clear ( ) | 将set中的元素清空 |
iterator find ( constkey_type& x ) const | 返回set中值为x的元素的位置 |
size_type count ( constkey_type& x ) const | 返回set中值为x的元素的个数 |
使用方法不在多说,基本顾名思义!!! 关于容量的操作:
函数声明 | 功能介绍 |
|---|---|
bool empty ( ) const | 检测set是否为空,空返回true,否则返回true |
size_type size() const | 返回set中有效元素的个数 |
迭代器也与之前学习的大差不差!!! 就不在细说!!!
这个很简单,就是允许重复的set
Map Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.
In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both: typedef pair<const Key, T> value_type;
Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
map containers aregenerally slower than unordered_map containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.
The mapped values in a map can be accessed directly by their corresponding key using the bracket operator((operator[]).
Maps are typically implemented as binary search trees
总结:
map是关联容器,它按照特定的次序(按照key来比较)存储由键值key和值value组合而成的元素。map中,键值key通常用于排序和惟一地标识元素,而值value中存储与此键值key关联的内容。键值key和值value的类型可能不同,并且在map的内部,key与value通过成员类型value_type绑定在一起,为其取别名称为pair:typedef pair<const key, T> value_type;map中的元素总是按照键值key进行比较排序的。map中通过键值访问单个元素的速度通常比unordered_map容器慢,但map允许根据顺序对元素进行直接迭代(即对map中的元素进行迭代时,可以得到一个有序的序列)。map支持下标访问符,即在[ ]中放入key,就可以找到与key对应的value。map通常被实现为二叉搜索树(更准确的说:平衡二叉搜索树(红黑树))。先来看模版参数:

key:键值对中key的类型
T: 键值对中value的类型
Compare: 比较器的类型,map中的元素是按照key来比较的,缺省情况下按照小于来比较,一般情况下(内置类型元素)该参数不需要传递,如果无法比较时(自定义类型),需要用户自己显式传递比较规则(一般情况下按照函数指针或者仿函数来传递)
Alloc:通过空间配置器来申请底层空间,不需要用户传递,除非用户不想使用标准库提供的空间配置器
大部分与set类似,插入传入的参数是pair<const key, T>
迭代器也大差不差,不在细说!!!
Multimaps是关联式容器,它按照特定的顺序,存储由key和value映射成的键值对<key,value>,其中多个键值对之间的key是可以重复的。就是可以重复的map
set通常用来去重,通常作为解题辅助,很重要哦!!!
接下来通过map来解决一些问题:

题目的意思就是让我们把如下的链表进行深拷贝

其中比较复杂的是random的处理!!!因为指向的很复杂所以不好解决!!!
但是通过map就可以很好解决!
map的映射来储存当前被复制节点的新节点被复制节点的random的映射节点,处理random就可以了Node* copyRandomList(Node* head) {
//map解决
map<Node* , Node*> nodemap;
//创建新链表
Node* rethead = nullptr;
Node* rettail = nullptr;
Node* cur = head;
while(cur != nullptr)
{
//如果为空就设置初始值
if(rethead == nullptr) rethead = rettail = new Node(cur->val);
else
{
Node* node = new Node(cur->val);
rettail->next = node;
//node->random = map1[cur];
rettail = node;
}
//原节点与复制节点产生映射关系
nodemap[cur] = rettail;
cur = cur->next;
}
//处理random
//nodemap记录了原本指针与新指针的映射关系
//我们需要通过原本指针来找到当前random指针
cur = head;
Node* copy = rethead;
while(cur != nullptr)
{
//找到random节点即可
copy->random = nodemap[cur->random];
copy = copy->next;
cur = cur->next;
}
return rethead;
}提交:过啦!!!

题目描述,很好理解。我们需要按照频率由高到低排序。如果不同的单词有相同出现频率, 按字典顺序 排序的规律来取出前k个元素
通过使用map可以很简单的解决问题
struct kvcom
{
bool operator()(const pair<string , int>& a , const pair<string , int>& b)
{
return a.second > b.second || (a.second == b.second && a.first < b.first);
//return a.second > b.second;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
//map解决
//先统计次数
map<string , int> wordsmap;
for(auto s : words)
{
wordsmap[s]++;
}
//按照次数排序
vector<pair<string , int>> v(wordsmap.begin() , wordsmap.end());
//或者直接使用稳定的排序来处理
//stable_sort(v.begin() , v.end(), kvcom());
sort(v.begin() , v.end(), kvcom());
vector<string> ret;
for(int i = 0 ; i < k ; i++)
{
ret.push_back(v[i].first) ;
}
return ret;
}相信大家注意到了什么代码还要一个版本被我们注释了:
使用stable_sort是因为,遍历计数的时候其实就已经在map内部满足字典序。所以在按照次数排序的话就需要稳定的排序来保证原本的相对位置不会改变!!!
这两个版本都可以完美通过!!!