在C++ STL中设置
标准模板库(STL)包含内置的数据结构,在实现复杂算法时会派上用场。
这些容器之一是"集合"。
C++中集合的属性
让我们先介绍C++集的一些基本属性,然后再介绍其实现。
唯一性– C++集内的所有元素都是唯一的。
已排序–集合中的元素始终是已排序的方式。
不可变–集合内的任何元素都无法更改。
它只能被插入或者删除。未索引-STL集不支持索引。
内部实现– STL中的集合由BST(二进制搜索树)在内部实现。
用C++初始化集合
Set的初始化涉及定义要存储的元素的数据类型以及存储它们的顺序。
#include<iostream>
#include<set>
/*
Or
#include<bits/stdc++.h>
*/
using namespace std;
int main(){
//Empty Set - Increasing Order (Default)
set<int> s1;
//s1 = {}
//Empty Set - Decreasing Order
set<int, greater<int>> s2;
//s2 = {}
//Set with values
set<int, greater<int>> s3 = {6, 10, 5, 1};
//s3 = {10, 6, 5, 1}
//Initialize Set using other set
set<int, greater<int>> s4(s3);
//s4 = {10, 6, 5, 1}
//Initializing a set from array
int arr[] = {10, 4, 5, 61};
set<int> s5(arr, arr+2); //Only two elements
//s5 = {4, 10}
return 1;
}
上面的代码段说明了初始化STL集的方法。
在C++中遍历集合
C++具有用于每个特定STL数据结构的迭代器的概念。
由于Set不支持索引,因此我们需要定义迭代器以获取其元素。
#include<iostream>
#include<set>
using namespace std;
int main(){
//Set with values
set<int, greater<int>> s1 = {6, 10, 5, 1};
//Iterator for the set
set<int> :: iterator it;
//Print the elements of the set
for(it=s1.begin(); it != s1.end();it++)
cout<<*it<<" ";
cout<<endl;
}
输出:
10 6 5 1
这里要注意的几件事是:
''begin()'`-此函数将迭代器返回到第一个元素。
'end()'–此函数返回最后一个元素之后的迭代器。'* it'–存放在位置迭代器中的值。
向集合添加元素
将元素添加到集合中的琐碎任务是通过insert()函数完成的。
该函数采用与集合中元素相同的值。
#include<iostream>
#include<set>
using namespace std;
int main(){
//Set with values
set<int, greater<int>> s1 = {6, 10, 5, 1};
//s1 = {10, 6, 5, 1}
//Inserting elements in the set
s1.insert(12);
s1.insert(20);
s1.insert(3);
//Iterator for the set
set<int> :: iterator it;
//Print the elements of the set
for(it=s1.begin(); it != s1.end();it++)
cout<<*it<<" ";
cout<<endl;
}
输出:
20 12 10 6 5 3 1
要记住的一件事是,添加的元素是根据创建集合时定义的排列来放置的。
因此,将元素插入集合的时间复杂度为O(logN),其中N是集合的大小。
集合的大小可以通过'size()'函数来获取。
从集合中删除元素
STL集支持erase()函数,该函数可以将值或者迭代器带到要从集中删除的值。
#include<iostream>
#include<set>
using namespace std;
int main(){
//Set with values
set<int, greater<int>> s1 = {6, 10, 5, 1};
//s1 = {10, 6, 5, 1}
//Erasing element value = 1
s1.erase(1);
//s1 = {10, 6, 5}
//Erasing the first element
s1.erase(s1.begin());
//s1 = {6, 5}
//Iterator for the set
set<int> :: iterator it;
//Print the elements of the set
for(it=s1.begin(); it != s1.end();it++)
cout<<*it<<" ";
cout<<endl;
}
输出:
6 5
如果集合中不存在要删除的元素,则C++代码不会提示错误。
如果传递了无效的迭代器,则会引发运行时错误。
与insert()函数类似,erase()函数花费O(logN)时间删除元素。
搜索集合中的元素
有一个预定义的函数" find()",用于在集合中搜索元素。
如果存在,则该函数将迭代器返回到该值,否则返回最后一个元素(" end()")之后的迭代器。
#include<iostream>
#include<set>
using namespace std;
int main(){
//Set with values
set<int, greater<int>> s1 = {6, 10, 5, 1};
//s1 = {10, 6, 5, 1}
//The value to be searched
int val = 5;
//Check if the iterator returned is not the ending of set
if(s1.find(val) != s1.end())
cout<<"The set contains "<<val<<endl;
else
cout<<"The set does not contains "<<val<<endl;
//The value to be searched
val = 11;
//Check if the iterator returned is not the ending of set
if(s1.find(val) != s1.end())
cout<<"The set contains "<<val<<endl;
else
cout<<"The set does not contains "<<val<<endl;
}
输出:
The set contains 5 The set does not contains 11
该代码是不言自明的,因为它根据值的存在或者不存在来打印消息。
其他常用设定功能
以下是一些使用STL Set时可能会派上用场的Set函数。
size()–返回集合的大小empty()–如果Set为空,则返回true,否则返回false。rbegin()-返回指向Set的最后一个元素的反向迭代器。
rend()–返回指向Set的第一个元素之前的反向迭代器。clear()–清空整个Set。
count(value)–如果集合中存在" value",则返回1,否则返回0。swap(set1,set2)–交换两个集合的内容。
emplace(value)-将"值"插入到集合中(如果不存在)。

