文章目录
- [1. map系列的使用](#1. map系列的使用)
-
- [1.1 map和multimap参考文档](#1.1 map和multimap参考文档)
- [1.2 map类的介绍](#1.2 map类的介绍)
- [1.3 pair类型介绍](#1.3 pair类型介绍)
- [1.4 map的构造](#1.4 map的构造)
- [1.5 map的增删查](#1.5 map的增删查)
-
- [1.5.1 插入](#1.5.1 插入)
- [1.6 map的数据修改](#1.6 map的数据修改)
- [1.7 构造遍历及增删查使用样例](#1.7 构造遍历及增删查使用样例)
- [1.8 map的迭代器和\[\]功能样例](#1.8 map的迭代器和[]功能样例)
-
- [补充 map::at](#补充 map::at)
- [1.9 multimap和map的差异](#1.9 multimap和map的差异)
- [1.10 138. 随机链表的复制(https://leetcode.cn/problems/copy-list-with-random-pointer/description/) - 力扣(LeetCode)](#1.10 138. 随机链表的复制 - 力扣(LeetCode))
- [1.11 692. 前K个高频单词(https://leetcode.cn/problems/top-k-frequent-words/) - 力扣(LeetCode)](#1.11 692. 前K个高频单词 - 力扣(LeetCode))
1. map系列的使用
1.1 map和multimap参考文档
1.2 map类的介绍
map的声明如下,Key就是map底层关键字的类型,T是map底层value的类型,set默认要求Key支持小于比较,如果不支持或者需要的话可以自行实现仿函数传给第二个模板参数,map底层存储数据的内存是从空间配置器申请的。一般情况下,我们都不需要传后两个模板参数。map底层是用红黑树实现,增删查改效率是
O(logN),迭代器遍历是走的中序,所以是按key有序顺序遍历的。
cpp
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
1.3 pair类型介绍
map底层的红黑树节点中的数据,使用pair<Key, T>存储键值对数据。

cpp
typedef pair<const Key, T> value_type;
template <class T1, class T2>
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)
{}
template<class U, class V>
pair (const pair<U,V>& pr): first(pr.first), second(pr.second)
{}
};
template <class T1,class T2>
inline pair<T1,T2> make_pair (T1 x, T2 y)
{
return ( pair<T1,T2>(x,y) );
}
1.4 map的构造
map的构造我们关注以下几个接口即可。
map的支持正向和反向迭代遍历,遍历默认按key的升序顺序,因为底层是二叉搜索树,迭代器遍历走的中序;支持迭代器就意味着支持范围for,map支持修改value数据,不支持修改key数据,修改关键字数据,破坏了底层搜索树的结构。
cpp
// empty (1) 无参默认构造
explicit map (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
// range (2) 迭代器区间构造
template <class InputIterator>
map (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& = allocator_type());
// copy (3) 拷贝构造
map (const map& x);
// initializer list (5) initializer 列表构造
map (initializer_list<value_type> il,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
// 迭代器是一个双向迭代器
iterator -> a bidirectional iterator to const value_type
// 正向迭代器
iterator begin();
iterator end();
// 反向迭代器
reverse_iterator rbegin();
reverse_iterator rend();
1.5 map的增删查
map的增删查关注以下几个接口即可:
map增接口,插入的pair键值对数据,跟set所有不同,但是查和删的接口只用关键字key跟set是完全类似的,不过find返回iterator,不仅仅可以确认key在不在,还找到key映射的value,同时通过迭代还可以修改value
cpp
Member types
key_type -> The first template parameter (Key)
mapped_type -> The second template parameter (T)
value_type -> pair<const key_type,mapped_type>
// 单个数据插入,如果已经key存在则插入失败,key存在相等value不相等也会插入失败
pair<iterator,bool> insert (const value_type& val);
// 列表插入,已经在容器中存在的值不会插入
void insert (initializer_list<value_type> il);
// 迭代器区间插入,已经在容器中存在的值不会插入
template <class InputIterator>
void insert (InputIterator first, InputIterator last);
// 查找k,返回k所在的迭代器,没有找到返回end()
iterator find (const key_type& k);
// 查找k,返回k的个数
size_type count (const key_type& k) const;
// 删除一个迭代器位置的值
iterator erase (const_iterator position);
// 删除k,k存在返回0,存在返回1
size_type erase (const key_type& k);
// 删除一段迭代器区间的值
iterator erase (const_iterator first, const_iterator last);
// 返回大于等k位置的迭代器
iterator lower_bound (const key_type& k);
// 返回大于k位置的迭代器
const_iterator lower_bound (const key_type& k) const;
1.5.1 插入
C++98的插入方式



C++11

两句代码调用的函数不一样
| 代码 | 外层 {} 的含义 | 内层 {} 的含义 | 匹配的重载 |
|---|---|---|---|
{ "right", "右边" } |
构造一个 pair | 无内层 | 重载A(单个元素) |
{ {"string","字符串"}, {"map","地图,映射"} } |
构造 initializer_list | 每个内层构造一个 pair | 重载B(多个元素) |
initializer_list 可以用来插入一个元素,只不过需要写成两层花括号
key相同不插入
代价:如果字符串很长,拷贝两个 string 涉及堆内存分配和字符复制,开销显著。
1.6 map的数据修改
前面我提到map支持修改mapped_type数据,不支持修改key数据,修改关键字数据,破坏了底层搜索树的结构。
map第一个支持修改的方式时通过迭代器,迭代器遍历时或者find返回key所在的iterator修改,map还有一个非常重要的修改接口operator\[\],但是operator\[\]不仅仅支持修改,还支持插入数据和查找数据,所以他是一个多功能复合接口
需要注意从内部实现角度,map这里把我们传统说的value值,给的是T类型,typedef为mapped_type。而value_type是红黑树结点中存储的pair键值对值。日常使用我们还是习惯将这边的T映射值叫做value。
cpp
Member types
key_type -> The first template parameter (Key)
mapped_type -> The second template parameter (T)
value_type -> pair<const key_type,mapped_type>
// 查找k,返回k所在的迭代器,没有找到返回end(),如果找到了通过iterator可以修改key对应的
mapped_type值
iterator find (const key_type& k);
// 文档中对insert返回值的说明
// The single element versions (1) return a pair, with its member pair::first
set to an iterator pointing to either the newly inserted element or to the
element with an equivalent key in the map. The pair::second element in the pair
is set to true if a new element was inserted or false if an equivalent key
already existed.
// insert插入一个pair<key, T>对象
// 1、如果key已经在map中,插入失败,则返回一个pair<iterator,bool>对象,返回pair对象
first是key所在结点的迭代器,second是false
// 2、如果key不在在map中,插入成功,则返回一个pair<iterator,bool>对象,返回pair对象
first是新插入key所在结点的迭代器,second是true
// 也就是说无论插入成功还是失败,返回pair<iterator,bool>对象的first都会指向key所在的迭
代器
// 那么也就意味着insert插入失败时充当了查找的功能,正是这一点,insert可以用来实现
operator[]
// 需要注意的是这里有两个pair,不要混淆了,一个是map底层红黑树节点中存的pair<key, T>,另
一个是insert返回值pair<iterator,bool>
pair<iterator,bool> insert (const value_type& val);
mapped_type& operator[] (const key_type& k);
// operator的内部实现
mapped_type& operator[] (const key_type& k)
{
// 1、如果k不在map中,insert会插入k和mapped_type默认值,同时[]返回结点中存储
mapped_type值的引用,那么我们可以通过引用修改返映射值。所以[]具备了插入+修改功能
// 2、如果k在map中,insert会插入失败,但是insert返回pair对象的first是指向key结点的
迭代器,返回值同时[]返回结点中存储mapped_type值的引用,所以[]具备了查找+修改的功能
pair<iterator, bool> ret = insert({ k, mapped_type() });
iterator it = ret.first;
return it->second;
}
1.7 构造遍历及增删查使用样例
cpp
#include<iostream>
#include<map>
using namespace std;
int main()
{
// initializer_list构造及迭代遍历
map<string, string> dict = { {"left", "左边"}, {"right", "右边"},
{"insert", "插入"},{ "string", "字符串" } };
//map<string, string>::iterator it = dict.begin();
auto it = dict.begin();
while (it != dict.end())
{
//cout << (*it).first <<":"<<(*it).second << endl;
// map的迭代基本都使用operator->,这里省略了一个->
// 第一个->是迭代器运算符重载,返回pair*,第二个箭头是结构指针解引用取pair数据
//cout << it.operator->()->first << ":" << it.operator->()-
>second << endl;
cout << it->first << ":" << it->second << endl;
++it;
}
cout << endl;
// insert插入pair对象的4种方式,对比之下,最后一种最方便
pair<string, string> kv1("first", "第一个");
dict.insert(kv1);
dict.insert(pair<string, string>("second", "第二个"));
dict.insert(make_pair("sort", "排序"));
dict.insert({ "auto", "自动的" });
// "left"已经存在,插入失败
dict.insert({ "left", "左边,剩余" });
// 范围for遍历
for (const auto& e : dict)
{
cout << e.first << ":" << e.second << endl;
}
cout << endl;
string str;
while (cin >> str)
{
auto ret = dict.find(str);
if (ret != dict.end())
{
cout << "->" << ret->second << endl;
}
else
{
cout << "无此单词,请重新输入" << endl;
}
}
return 0;
}
cpp
// 结构化绑定 C++17才有
auto[x, y] = kv1;
//for (auto [k, v] : dict) 错,拷贝代价大
//for (auto& [k, v] : dict)
for (const auto&[k,v] : dict) //const可加可不加
{
cout << k << ":" << v << endl;
}
cout << endl;
//找到后删除
auto pos = dict.find("left");
if(pos != dict.end())
{
dict.erase(pos);
}
for (const auto& [k, v] : dict)
{
cout << k << ":" << v << endl;
}
cout << endl;
//也可以直接删除
dict.erase("left");
lower_bound(),upper_bound()找一个左闭右开的区间(和set一样)
1.8 map的迭代器和\[\]功能样例
operator\[\]是map的考点
cpp
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
// 利用find和iterator修改功能,统计水果出现的次数
string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜",
"苹果", "香蕉", "苹果", "香蕉" };
map<string, int> countMap;
//for (auto& e : arr)
//{
// /*auto it = countMap.find(e);
// 先查找水果在不在map中
// 1、不在,说明水果第一次出现,则插入{水果, 1}
// 2、在,则查找到的节点中水果对应的次数++
// if (it != countMap.end())
// {
// it->second++;
// }
// else
// {
// countMap.insert({ e, 1 });
// }*/
// countMap[e]++; //或者只写这一句
//}
for (auto& e : arr)
{
countMap[e]++;
}
for (auto& [k, v] : countMap)
{
cout << k << ":" << v << endl;
}
cout << endl;
return 0;
}

operator\[\]三大功能
cpp
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
map<string, string> dict;
dict.insert(make_pair("sort", "排序"));
// key不存在->插入 {"insert", string()}
dict["insert"];
// 插入+修改
dict["left"] = "左边";
// 修改
dict["left"] = "左边、剩余";
// key存在->查找
//但是没有的话会插入
cout << dict["left"] << endl;
return 0;
}
补充 map::at
C++11才有,纯粹的查找+修改

cpp
dict.at("left") = "xxxxx";
// key不存在,会抛异常
// dict.at("insert") = "xxxxx";
1.9 multimap和map的差异
multimap也在<map>库里面
multimap和map的使用基本完全类似,主要区别点在于multimap支持关键值key冗余,那么insert/find/count/erase都围绕着支持关键值key冗余有所差异,这里跟set和multiset完全一样,比如find时,有多个key,返回中序第一个。其次就是multimap不支持\[\],因为支持key冗余,\[\]就只能支持插入了,不能支持修改。
c
multimap<string, string> dict;
dict.insert({ "right", "右边" });
dict.insert({ "left", "左边" });
dict.insert({ "right", "右边xx" });
dict.insert({ "right", "右边" });
for (const auto& [k, v] : dict)
{
cout << k << ":" << v << endl;
}
cout << endl;
multmap区别
- find找中序第一个
- 没有operator\[\]
1.10 138. 随机链表的复制 - 力扣(LeetCode)
数据结构初阶阶段,为了控制随机指针,我们将拷贝结点链接在原节点的后面解决,后面拷贝节点还得解下来链接,非常麻烦。这里我们直接让<原结点,拷贝结点>建立映射关系放到map中,控制随机指针会非常简单方便,这里体现了map在解决一些问题时的价值,完全是降维打击。
原本:

cpp
class Solution {
public:
Node* copyRandomList(Node* head) {
map<Node*, Node*> nodeMap;
Node* copyhead = nullptr,*copytail = nullptr;
Node* cur = head;
//深拷贝链表
while(cur)
{
if(copytail == nullptr)
{
copyhead = copytail = new Node(cur->val);
}
else
{
copytail->next = new Node(cur->val);
copytail = copytail->next;
}
// 原节点和拷贝节点map kv存储
nodeMap[cur] = copytail;
cur = cur->next;
}
// 处理random
cur = head;
Node* copy = copyhead;
while(cur)
{
if(cur->random == nullptr)
{
copy->random = nullptr;
}
else
{
//查找
copy->random = nodeMap[cur->random];
}
cur = cur->next;
copy = copy->next;
}
return copyhead;
}
};
1.11 692. 前K个高频单词 - 力扣(LeetCode)
本题我们利用map统计出次数以后,返回的答案应该按单词出现频率由高到低排序,有一个特殊要求,如果不同的单词有相同出现频率,按字典顺序排序。
解决思路1:
用排序找前k个单词,因为map中已经对key单词排序过,也就意味着遍历map时,次数相同的单词,字典序小的在前面,字典序大的在后面。那么我们将数据放到vector中用一个稳定的排序就可以实现上面特殊要求,但是sort底层是快排,是不稳定的,所以我们要用stable_sort,它是稳定的。
稳定性:排序后,两个数相对次序不变就说明稳定,例如冒泡,插入,归并排序。没有方法能把不稳定变为稳定
cpp
class Solution {
public:
struct Compare
{
bool operator()(const pair<string, int>& x, const pair<string, int>& y)
const
{
return x.second > y.second;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
map<string, int> countMap;
for(auto& e : words)
{
countMap[e]++;
}
//拷贝构造
vector<pair<string, int>> v(countMap.begin(), countMap.end());
// 仿函数控制降序
stable_sort(v.begin(), v.end(), Compare());
//sort(v.begin(), v.end(), Compare());
// 取前k个放到strV
vector<string> strV;
for(int i = 0; i < k; ++i)
{
strV.push_back(v[i].first);
}
return strV;
}
};
解决思路2:
将map统计出的次数的数据放到vector中排序,或者放到priority_queue中来选出前k个。利用仿函数强行控制次数相等的,字典序小的在前面。
cpp
class Solution {
public:
struct Compare
{
bool operator()(const pair<string, int>& x, const pair<string, int>& y) const
{
return x.second > y.second || (x.second == y.second && x.first < y.first);;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
map<string, int> countMap;
for(auto& e : words)
{
countMap[e]++;
}
vector<pair<string, int>> v(countMap.begin(), countMap.end());
// 仿函数控制降序,仿函数控制次数相等,字典序小的在前面
//时间复杂度O(logN)
sort(v.begin(), v.end(), Compare());
// 取前k个
vector<string> strV;
for(int i = 0; i < k; ++i)
{
strV.push_back(v[i].first);
}
return strV;
}
};
解决思路3:建大堆
cpp
class Solution {
public:
struct Compare
{
bool operator()(const pair<string, int>& x, const pair<string, int>& y)
const
{
// 要注意优先级队列底层是反的,大堆要实现小于比较,所以这里次数相等,想要字典序小的在前面要比较字典序大的为真
return x.second < y.second || (x.second == y.second && x.first > y.first);
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
map<string, int> countMap;
for(auto& e : words)
{
countMap[e]++;
}
// 将map中的<单词,次数>放到priority_queue中,仿函数控制大堆,次数相同按照字典序规则排序
priority_queue<pair<string, int>, vector<pair<string, int>>, Compare>
p(countMap.begin(), countMap.end());
vector<string> strV;
for(int i = 0; i < k; ++i)
{
strV.push_back(p.top().first);
p.pop();
}
return strV;
}
};