目录
我也从没想过STL中的set还有这么多的花样。总而言之,如果你觉得这篇文章还不错,劳烦多多支持一下!码字不易,感谢你的观看!
前言
这个栏目是对我算法学习过程的同步记录,我也希望能够通过这个专栏加深自己对编程的理解以及帮助到更多像我一样想从零学习算法并参加竞赛的同学。在这个专栏的文章中我会结合在编程过程中遇到的各种问题并提出相应的解决方案。当然,如果屏幕前的你有更好的想法或者发现的错误也欢迎交流和指出!不喜勿喷!不喜勿喷!不喜勿喷!那么事不宜迟,我们马上开始吧!
一、set(集合)
1.基本介绍
set是一种容器,用于存储一组唯一的元素,并按照一定的规则排序。set中元素默认是按升序排列的。默认情况下使用元素的比较运算符(<)来进行排序。
set的示意图如下所示:
2.set的定义与结构
set的结构和定义如下:
template <class Key, class Compare = less<Key>,
class Allocator = allocator<Key>>
class set;
(1)Key: 表示存储在set中元素的类型。
(2)Compare: 表示元素之间比较函数对象的类型,默认为less,即按照元素的值进行比较。
(3)Allocator: 表示用于分配内存的分配器类型,默认为allocator。
(4)内部机制:set的内部实现了用红黑树(一种自平衡的二叉搜索树)来存储元素。,并保持元素的有序性。这使得在set中插入、删除和查找元素的时间复杂度是非常优秀的对数时间,即O(logn)。
(5)特性:set中的元素是唯一的。当插入一个重复的元素时,set会将其自动忽略。
3.set的常用函数
set的常用函数表如下所示:
函数 | 描述 | 平均时间复杂度 | 最坏时间复杂度 |
---|---|---|---|
insert(x) | 向集合中插入元素 | O(logn) | ~ |
erase(x) | 从集合中移除元素 | O(logn) | ~ |
find(x) | 查找集合中的元素 | O(logn) | ~ |
lower_bound(x) | 返回第一个不小于给定值的迭代器 | O(logn) | ~ |
upper_bound(x) | 返回第一个大于给定值的迭代器 | O(logn) | ~ |
equal_range | 返回一个范围,其中包含所有给定值的元素 | O(logn) | ~ |
size | 返回集合中元素的数量 | O(1) | ~ |
empty | 检查集合是否为空 | O(1) | ~ |
clear | 清空集合 | O(n) | ~ |
begin | 返回指向集合起始位置的迭代器 | O(1) | ~ |
end | 返回指向集合末尾位置的迭代器 | O(1) | ~ |
rbegin | 返回指向集合末尾的逆向迭代器 | O(1) | ~ |
rend | 返回指向集合起始位置的逆向迭代器 | O(1) | ~ |
swap | 交换两个集合 | O(1) | ~ |
4.修改set比较方式的常见手段
set的比较方式如下所示,后面的multiset类似。
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
set<int, greater<int>>set1;
set1.insert(25);
set1.insert(26);
set1.insert(21);
set1.insert(36);
for (auto elem : set1)cout << elem << " ";
cout << endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct compare
{
bool operator()(int a,int b)
{
return a > b;
}
};
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
set<int, compare>set1;
set1.insert(25);
set1.insert(26);
set1.insert(21);
set1.insert(36);
for (auto elem : set1)cout << elem << " ";
cout << endl;
return 0;
}
二、multiset多重集合
1.简要介绍
multiset与set的不同之处在于其允许存放重复的元素。
2.multiset的常用函数
multiset的常用函数与set基本相同。
三、unordered_set无序集合
1.简要介绍
unordered_set与set的区别在于其中存放的元素没有固定的顺序。其使用哈希表来实现元素的存储与访问,因此元素的插入、删除和查找的品骏时间复杂度非常优秀,为O(1)。
但是由于其时间复杂度具有不确定性,我们在竞赛中一般很少使用,了解即可。
2.unordered_set的常用函数
unordered_set的常用函数表如下所示:
函数 | 描述 | 平均时间复杂度 | 最坏时间复杂度 |
---|---|---|---|
insert | 向无序集合中插入元素 | O(1) | O(n) |
erase | 从无序集合中移除元素 | O(1) | O(n) |
find | 查找无需集合中的元素 | O(1) | O(n) |
count | 返回元素在集合中出现的次数 | O(1) | O(n) |
size | 返回无序集合中元素的数量 | O(1) | O(1) |
四、代码实战
1.set
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
set<int>set1;
set1.insert(5);
set1.insert(2);
set1.insert(8);
set1.insert(5);//重复元素
//遍历集合
cout << "Set elements:" << endl;
for (auto elem : set1)cout << elem << " ";
cout << endl;
//查找元素
int searchvalue = 5;
auto it = set1.find(searchvalue);
if (it != set1.end())cout << searchvalue << " is found" << endl;
else cout << "Not found" << endl;
//删除元素
int removevalue = 2;
set1.erase(removevalue);
//再次遍历集合
cout << "Set elements:" << endl;
for (auto elem : set1)cout << elem << " ";
cout << endl;
//清空集合
set1.clear();
//检查集合是否为空
if (set1.empty())cout << "empty" << endl;
else cout << "not empty" << endl;
return 0;
}
2.mulitiset
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
multiset<int>set1;
set1.insert(5);
set1.insert(2);
set1.insert(8);
set1.insert(5);//允许重复元素
//遍历集合
cout << "Set elements:" << endl;
for (auto elem : set1)cout << elem << " ";
cout << endl;
//查找元素
int searchvalue = 5;
auto it = set1.find(searchvalue);
if (it != set1.end())cout << searchvalue << " is found" << endl;
else cout << "Not found" << endl;
//删除元素
int removevalue = 2;
set1.erase(removevalue);
//再次遍历集合
cout << "Set elements:" << endl;
for (auto elem : set1)cout << elem << " ";
cout << endl;
//清空集合
set1.clear();
//检查集合是否为空
if (set1.empty())cout << "empty" << endl;
else cout << "not empty" << endl;
return 0;
}
3.unordered_set
代码如下:
#include<bits/stdc++.h>
#include<unordered_set>
using namespace std;
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
unordered_set<int>set1;
set1.insert(5);
set1.insert(2);
set1.insert(8);
set1.insert(5);//允许重复元素
//遍历集合
cout << "Set elements:" << endl;
for (auto elem : set1)cout << elem << " ";
cout << endl;
//查找元素
int searchvalue = 5;
auto it = set1.find(searchvalue);
if (it != set1.end())cout << searchvalue << " is found" << endl;
else cout << "Not found" << endl;
//删除元素
int removevalue = 2;
set1.erase(removevalue);
//再次遍历集合
cout << "Set elements:" << endl;
for (auto elem : set1)cout << elem << " ";
cout << endl;
//清空集合
set1.clear();
//检查集合是否为空
if (set1.empty())cout << "empty" << endl;
else cout << "not empty" << endl;
return 0;
}
记得引用<unordered_set>头文件!!