欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 【C++】map和set的使用

【C++】map和set的使用

2024/10/26 16:12:24 来源:https://blog.csdn.net/2302_81580770/article/details/143091898  浏览:    关键词:【C++】map和set的使用

最好的,不一定是最合适的;最合适的,才是真正最好的。💓💓💓

目录

  •✨说在前面

•🌰序列式容器和关联式容器

🍋知识点一:set系列的使用

•🌰1.set类的介绍

•🌰2.set的构造和迭代器

•🌰3.set的增删查

•🌰4.insert和迭代器遍历使用样例

•🌰5.find和erase使用样例

•🌰6.lower_bound和upper_bound使用样例

•🌰7.multiset和set的差异

•🌰349. 两个数组的交集 - 力扣(LeetCode)

•🌰142. 环形链表 II - 力扣(LeetCode)

🍋知识点二:map系列的使用

•🌰1.map与pair介绍

•🌰2.map的构造

•🌰3.map的增删查

•🌰4.map的数据修改

•🌰5.构造遍历、增删查使用举例

•🌰6.map的迭代器和[]功能样例

•🌰7.multimap和map的差异

•🌰138. 随机链表的复制 - 力扣(LeetCode)

•🌰692. 前K个高频单词 - 力扣(LeetCode)

 • ✨SumUp结语


  •✨说在前面

亲爱的读者们大家好!💖💖💖,我们又见面了,上一篇文章我给大家详细讲解了C++中的搜索二叉树。如果大家没有掌握好相关的知识,上一篇篇文章讲解地很详细,可以再回去看看,复习一下,再进入今天的内容。

我们之前给大家讲解过搜索二叉树的内容,我们今天给大家讲解map和set的使用。map和set的底层容器是红黑树,也是搜索二叉树,如果大家准备好了,那就接着往下看吧~

   👇👇👇
💘💘💘知识连线时刻(直接点击即可)

【C++】二叉搜索树

  🎉🎉🎉复习回顾🎉🎉🎉

         

 博主主页传送门:愿天垂怜的博客

 ​​

 

•🌰序列式容器和关联式容器

前面我们已经接触过STL中的部分容器如:string、vector、list、deque、array、forward_list等,这些容器统称为序列式容器,因为逻辑结构为线性序列的数据结构两个位置存储的值之间一般没有紧密的关联关系,比如交换一下,他依旧是序列式容器。顺序容器中的元素是按他们在容器中的存储位置来顺序保存和访问的。

关联式容器也是用来存储数据的,与序列式容器不同的是,关联式容器逻辑结构通常是非线性结构两个位置有紧密的关联关系,交换一下,他的存储结构就被破坏了。顺序容器中的元素是按关键字来保存和访问的。关联式容器有map/set系列和unordered_map/unordered_set系列。

本章节讲解的map和set底层是红黑树,红黑树是一颗平衡二叉搜索树。set是key搜索场景的结构,map是key/value搜索场景的结构。

🍋知识点一:set系列的使用

•🌰1.set类的介绍

set和multiset参考文档:<set> - C++ Reference

1. set的声明如下,T就是set底层关键字的类型

2. set默认要求T支持小于比较,如果不支持或者想按自己的需求走可以自行实现仿函数传给第二个模版参数

3. set底层存储数据的内存是从空间配置器申请的,如果需要可以自己实现内存池,传给第三个参

数。

4. 一般情况下,我们都不需要传后两个模版参数。

5. set底层是用红黑树实现,增删查效率是O(logN)迭代器遍历是走的搜索树的中序,所以是有序的

6. 前面部分我们已经学习了vector/list等容器的使用,STL容器接口设计,高度相似,所以这里我们就不再一个接口语一个接口的介绍,而是直接看文档,挑比较重要的接口进行介绍。

template < class T,				// set::key_type/value_typeclass Compare = less<T>,	// set::key_compare/value_compareclass Alloc = allocator<T>	// set::allocator_type
> class set;

 

•🌰2.set的构造和迭代器

set的构造我们关注以下几个接口即可。

set的支持正向和反向迭代遍历,遍历默认按升序顺序,因为底层是二叉搜索树,迭代器遍历走的中序;支持迭代器就意味着支持范围for,set的iterator和const_iterator都不支持迭代器修改数据,修改关键字数据,破坏了底层搜索树的结构。

//empty (1) 无参默认构造
explicit set(const key_compare& comp = key_compare(),const allocator_type& alloc = allocator_type());//range (2) 迭代器区间构造
template <class InputIterator>
set(InputIterator first, InputIterator last,const key_compare& comp = key_compare(),const allocator_type & = allocator_type());//copy (3) 拷⻉构造
set(const set& x);//initializer list (5) initializer 列表构造
set(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();

 

•🌰3.set的增删查

增删查我们主要关注以下接口:

Member types
key_type->The first template parameter(T)
value_type->The first template parameter(T)//单个数据插入,如果已经存在则插入失败
pair<iterator, bool> insert(const value_type& val);//列表插入,已经在容器中存在的值不会插入
void insert(initializer_list<value_type> il);
//迭代器区间插入,已经在容器中存在的值不会插入
template <class InputIterator>
void insert(InputIterator first, InputIterator last);//查找val,返回val所在的迭代器,没有找到返回end()
iterator find(const value_type& val);//查找val,返回val的个数
size_type count(const value_type& val) const;//删除一个迭代器位置的值
iterator erase(const_iterator position);//删除val,val不存在返回0,存在返回1
size_type erase(const value_type& val);//删除一段迭代器区间的值
iterator erase(const_iterator first, const_iterator last);//返回大于等val位置的迭代器
iterator lower_bound(const value_type& val) const;//返回大于val位置的迭代器
iterator upper_bound(const value_type& val) const;

 

•🌰4.insert和迭代器遍历使用样例

#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);s.insert(7);s.insert(3);set<int>::iterator it = s.begin();while (it != s.end()){cout << *it << " ";++it;}cout << endl;s.insert({ 2,8,3,9 });for (const auto& e : s){cout << e << " ";}cout << endl;set<string> strset = { "sort","insert","add" };//set<string> steset({ "sort","insert","add" });//遍历string是按照ascii码比较的for (auto& e : strset){cout << e << " ";}return 0;
}

 

•🌰5.find和erase使用样例

#include <iostream>
#include <set>
using namespace std;int main()
{set<int> s = { 4,2,7,2,8,5,9 };for (auto& e : s){cout << e << " ";}cout << endl;//删除最小值s.erase(s.begin());for (auto& e : s){cout << e << " ";}cout << endl;//直接删除int x;s.erase(x);if (s.count(x)){cout << x << " doesn't exist!" << endl;}else cout << x << " is successfully removed!" << endl;int y;cin >> y;auto pos = s.find(y);if (pos != s.end()){s.erase(pos);cout << y << " is successfully removed!" << endl;}else cout << y << " doesn't exist!" << endl;//算法库中的查找 O(N)auto pos1 = find(s.begin(), s.end(), x);//set自身的查找 O(logN)auto pos2 = s.find(x);return 0;
}

 

•🌰6.lower_bound和upper_bound使用样例

#include <iostream>
#include <set>
using namespace std;int main()
{set<int> myset;for (int i = 1; i < 10; i++){myset.insert(i * 10);}for (auto& e : myset){cout << e << " ";}cout << endl;//删除[25,55]的值//返回 >= 25auto itlow = myset.lower_bound(25);//返回 > 50auto itup = myset.upper_bound(50);//删除这段区间的值myset.erase(itlow, itup);for (auto& e : myset){cout << e << " ";}cout << endl;return 0;
}

 

•🌰7.multiset和set的差异

multiset的使用和set基本相似,主要区别点在于multiset支持值冗余,那么insert/find/count/erase都围绕着支持值冗余有所差异,具体参看下面的样例代码理解。

#include <iostream>
#include <set>
using namespace std;int main()
{//multiset排序,但是不去重multiset<int> s = { 4,2,7,2,4,8,4,5,4,9 };auto it = s.begin();while (it != s.end()){cout << *it << " ";++it;}cout << endl;//x可能会存在多个,find查找中序的第一个int x;cin >> x;/*auto pos = s.find(x);while (pos != s.end() && *pos == x){pos = s.erase(pos);}cout << endl;*/s.erase(x);it = s.begin();while (it != s.end()){cout << *it << " ";++it;}cout << endl;//与set不同的是,count会返回x的实际个数cout << s.count(5) << endl;return 0;
}

 

•🌰349. 两个数组的交集 - 力扣(LeetCode)

题目链接:349. 两个数组的交集 - 力扣(LeetCode)

题目描述:

思路:双指针法:

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {set<int> s1(nums1.begin(), nums1.end());set<int> s2(nums2.begin(), nums2.end());vector<int> ret;auto it1 = s1.begin();auto it2 = s2.begin();while(it1 != s1.end() && it2 != s2.end()){if(*it1 == *it2){ret.push_back(*it1);++it1;++it2;}else{if(*it1 < *it2)++it1;else ++it2;}}return ret;}
};

 注意:寻找交集、差集等集合间的关系在解决实际问题中有重要运用。

 

•🌰142. 环形链表 II - 力扣(LeetCode)

题目链接:142. 环形链表 II - 力扣(LeetCode)

题目描述:

数据结构初阶阶段,我们通过证明一个指针从头开始走一个指针从相遇点开始走,会在入口点相遇,理解证明都会很麻烦。这里我们使用set查找记录解决非常简单方便,这里体现了set在解决一些问题时的价值,完全是降维打击

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *detectCycle(ListNode *head) {set<ListNode*> s;ListNode* cur = head;while(cur){if(s.count(cur))return cur;elses.insert(cur);cur = cur->next;} return nullptr;}
};

 ​​

🍋知识点二:map系列的使用

•🌰1.map与pair介绍

map和multimap参考文档:<map> - C++ Reference

map类的介绍

map的声明如下,Key就是map底层关键字的类型,T是map底层value的类型,set默认要求Key支持小于比较,如果不支持或者需要的话可以自行实现仿函数传给第二个模版参数,map底层存储数据的内存是从空间配置器申请的。一般情况下,我们都不需要传后两个模版参数。map底层是用红黑树实现,增删查改效率是 O(logN) ,迭代器遍历是走的中序,所以是按key有序顺序遍历的。

template < class Key,			// map::key_typeclass T,					// map::mapped_typeclass Compare = less<Key>,	// map::key_compareclass Alloc = allocator<pair<const Key, T>> //map::allocator_type
>class map;

pair类的介绍

map底层的红黑树节点中的数据使用pair<Key, T>存储键值对数据

pair的底层实现如下:

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));
}

 

•🌰2.map的构造

map的构造我们关注以下几个接口即可。

map的支持正向和反向迭代遍历,遍历默认按key的升序顺序,因为底层是二叉搜索树,迭代器遍历走的中序;支持迭代器就意味着支持范围for,map支持修改value数据,不支持修改key数据,修改关键字数据,破坏了底层搜索树的结构。

//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();

 

•🌰3.map的增删查

map的增删查关注以下几个接口即可:

map增接口,插入的pair键值对数据,跟set所有不同,但是查和删的接口只用关键字key跟set是完全类似的,不过find返回iterator,不仅仅可以确认key在不在,还找到key映射的value,同时通过迭代还可以修改value

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;

 我们仔细看一下insert。

注意第一种,它的返回值是pair<iterator, bool>,也就是分为两种情况:

1. 插入成功,返回pair<新插入值所在的迭代器, true>

2. 插入失败,返回pair<已经存在的和key相等值的迭代器 , false> 

inset返回pair,说明insert不仅充当了插入的功能,还又查找的功能

 

•🌰4.map的数据修改

前面提到map支持修改mapped_type数据,不支持修改key数据,修改关键字数据,破坏了底层搜索树的结构。

map第一个支持修改的方式是通过迭代器,迭代器遍历时或者find返回key所在的iterator修改,map还有一个非常重要的修改接口operator[],但是operator[]不仅仅支持修改,还支持插入数据和查找数据,所以他是一个多功能复合接口需要注意从内部实现角度,map这里把我们传统说的value值,给的是T类型,typedef为mapped_type。而value_type是红黑树结点中存储的pair键值对值。日常使用我们还是习惯将这里的T映射值叫做value。

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;
}

 

•🌰5.构造遍历、增删查使用举例

#include <map>int main()
{//map<string, string> dict;map<string, string> dict = { {"left", "左边"}, {"right", "右边"}, {"insert", "插入"},{ "string", "字符串" } };pair<string, string> kv1("first", "第一个");dict.insert(kv1);dict.insert(pair<string, string>("second", "第二个"));dict.insert(make_pair("sort", "排序"));//C++11dict.insert({ "auto", "自动的" });//只看keydict.insert({ "auto", "自动的xxxxx" });map<string, string>::iterator it = dict.begin();while (it != dict.end()){//可以修改value,不支持修改key//it->first += 'x';it->second += 'x';//cout << (*it).first << (*it).second << endl;cout << it->first << ":" << it->second << endl;//cout << it.operator->()->first << ":" << it.operator->()->second << endl;++it;}cout << endl;return 0;
}

 

•🌰6.map的迭代器和[]功能样例

#include <iostream>
using namespace std;
#include <map>int main()
{//利用find和iterator,统计水果出现的次数string arr[] = { "苹果","西瓜","苹果","西瓜","苹果","苹果","西瓜","苹果","苹果","香蕉","苹果","香蕉" };map<string, int> countMap;for (const auto& str : arr){/*//查找水果在不在map中//1.不在,插入{水果,1}		//2.在,水果次数++auto ret = countMap.find(str);if (ret == countMap.end()){countMap.insert({ str, 1 });}else ret->second++;*/countMap[str]++;}for (auto& e : countMap){cout << e.first << ":" << e.second << endl;}return 0;
}
#include <iostream>
using namespace std;
#include <map>int main()
{map<string, string> dict;dict.insert(make_pair("sort", "排序"));//key不存在->插入{"insert", string()}dict["insert"];//key不存在->插入+修改dict["left"] = "左边";//key存在->修改dict["left"] = "左边,剩余";//查找,确定key存在才可以cout << dict["left"] << endl;//插入cout << dict["right"] << endl;return 0;
}

 

•🌰7.multimap和map的差异

multimap和map的使用基本完全类似,主要区别点在于multimap支持关键值key的冗余,那这样insert/find/count/erase都围绕着支持关键值key冗余有所差异,这里跟set和multiset完全一样,比如find时,有多个key,返回中序第一个。其次就是multimap不支持[],因为支持key冗余,[]就只能支持插入了,不能支持修改

#include <iostream>
using namespace std;
#include <map>int main()
{multimap<string, string> dict;dict.insert({ "sort", "排序0" });dict.insert({ "sort", "排序2" });dict.insert({ "sort", "排序1" });dict.insert({ "sort", "排序3" });dict.insert({ "string", "字符串" });for (auto& e : dict){cout << e.first << "" << e.second << endl;}dict.erase("sort");for (auto& e : dict){cout << e.first << "" << e.second << endl;}return 0;
}

 

•🌰138. 随机链表的复制 - 力扣(LeetCode)

题目链接:138. 随机链表的复制 - 力扣(LeetCode)

题目描述:

这是一道非常经典的考察链表的综合题。我们以前在链表的部分也是提供过一种思路,只不过略微复杂:

代码如下:

/*** Definition for a Node.* struct Node {*     int val;*     struct Node *next;*     struct Node *random;* };*/
typedef struct Node Node;
struct Node* copyRandomList(struct Node* head) {Node* cur = head;while (cur)//创建新节点并错位插入旧节点{Node* copy = (Node*)malloc(sizeof(Node));copy->val = cur->val;copy->next = cur->next;cur->next = copy;cur = copy->next;}cur = head;while (cur)//设置random指针的指向{Node* copy = cur->next;if (!cur->random)copy->random = NULL;else{copy->random = cur->random->next;}cur = copy->next;}cur = head;//创建新链表Node* newhead = (Node*)malloc(sizeof(Node));Node* newtail = newhead;while (cur)//将新节点尾插到新链表{Node* copy = cur->next;newtail->next = copy;newtail = newtail->next;cur->next = copy->next;//恢复原链表cur = copy->next;}if (newtail)newtail->next = NULL;return newhead->next;
}

 如今学习了map,我们可以有更简单的解法,即利用map将新的链表和旧的链表的地址作为map的K/V联系起来:

/*
// Definition for a Node.
class Node {
public:int val;Node* next;Node* random;Node(int _val) {val = _val;next = NULL;random = NULL;}
};
*/
class Solution {
public:Node* copyRandomList(Node* head) {map<Node*, Node*> nodeMap;Node* newhead = nullptr, *newtail = nullptr;Node* cur = head;while(cur){Node* copy = new Node(cur->val); if(newhead == nullptr)newhead = newtail = copy;else{newtail->next = copy;newtail = newtail->next;}  nodeMap[cur] = newtail;cur = cur->next;}cur = head;Node* copy = newhead;while(cur){if(cur->random == nullptr){copy->random = nullptr;}else{copy->random = nodeMap[cur->random];}cur = cur->next;copy = copy->next;}return newhead;}
};

•🌰692. 前K个高频单词 - 力扣(LeetCode)

题目链接:692. 前K个高频单词 - 力扣(LeetCode)

题目描述:

解决思路1:用排序找前k个单词,因为map中已经对key单词排序过,也就意味着遍历map时,次数相同的单词,字典序小的在前面,字典序大的在后面。那么我们将数据放到vector中用一个稳定的排序就可以实现上面特殊要求,但是大家注意,sort底层是快速排序,是不稳定的,所以我们要用stable_sort,他是稳定的

class Solution {
public:struct KVCompare{bool operator()(const pair<string, int>& kv1, const pair<string, int>& kv2){return kv1.second > kv2.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(), KVCompare());vector<string> strV;for(int i = 0; i < k; i++){strV.push_back(v[i].first);}return strV;}
};

当然,我们也可以通过修改仿函数控制比较逻辑:

class Solution {
public:struct KVCompare{bool operator()(const pair<string, int>& kv1, const pair<string, int>& kv2){return kv1.second > kv2.second || (kv1.second == kv2.second && kv1.first < kv2.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());//stable_sort(v.begin(), v.end(), KVCompare());sort(v.begin(), v.end(), KVCompare());vector<string> strV;for(int i = 0; i < k; i++){strV.push_back(v[i].first);}return strV;}
};

解决思路2:将map统计出的次数的数据放到vector中排序,或者放到priority_queue中来选出前k个。利用仿函数强行控制次数相等的,字典序小的在前面。

class Solution {
public:struct KVCompare{bool operator()(const pair<string, int>& kv1, const pair<string, int>& kv2){return kv1.second < kv2.second || (kv1.second == kv2.second && kv1.first > kv2.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());priority_queue<pair<string, int>, vector<pair<string, int>>, KVCompare> 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;}
};

 

 • ✨SumUp结语

到这里本篇文章的内容就结束了,本节介绍了C++中map和set的相关知识。这里的内容也是有一定难度的。希望大家能够认真学习,打好基础,迎接接下来的挑战,期待大家继续捧场~💖💖💖