set是STL中一种标准关联容器。它底层使用平衡的搜索树——红黑树实现,插入删除操作时仅仅需要指针操作节点即可完成,不涉及到内存移动和拷贝,所以效率比较高。在set中,每个元素都是唯一的,如果想要允许重复元素,可以使用multiset。
1.set的常用操作
使用时注意包含头文件<set> std::set and std::multiset associative containers
s.begin() 返回set容器的第一个元素
s.end() 返回set容器的最后一个元素
s.clear() 删除set容器中的所有的元素
s.empty() 判断set容器是否为空
s.insert() 插入一个元素
s.erase() 删除一个元素
s.size() 返回当前set容器中的元素个数
2.set容器的创建
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main(){
set<int> myset;
int a[5] = { 1, 2, 3, 4, 5 };
vector<int> vec{ 1, 2, 3, 4, 5 };
set<int> myset1(a, a+5);//根据数组地址初始化
set<int> myset2(vec.begin(), vec.end());//vector地址初始化
set<int> myset3(myset2.begin(), myset2.end());//set初始化
set<int> myset4(myset3); //复制初始化
system("pause");
return 0;
}
3.增删改查
插入
#include <iostream>
#include <set>
#include <vector>
using namespace std;
set<int> myset;
void printSet(int count){
cout << "Test output" << count << ":" << endl;
for (set<int>::iterator it = myset.begin(); it != myset.end(); it++){
cout << *it << " ";
}
cout << endl;
}
int main(){
int count = 1;
myset.insert(1);
myset.insert(2);
myset.insert(3);
printSet(count);
myset.insert(2); //不允许重复值出现
printSet(++count);
int a[4]={11, 12, 13, 14};
myset.insert(a, a + 4);
printSet(++count);
system("pause");
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200709183508834.png)
删除
s.erase() 清除某个元素
s.clear() 清除所有元素
#include <iostream>
#include <set>
#include <vector>
using namespace std;
set<int> myset;
void printSet(int count){
cout << "Test output" << count << ":" << endl;
for (set<int>::iterator it = myset.begin(); it != myset.end(); it++){
cout << *it << " ";
}
cout << endl;
}
int main(){
int count = 1;
for (int i = 1; i < 11; i++){
myset.insert(i);
}
printSet(count);
myset.erase(8);
printSet(++count);
set<int>::iterator ita = myset.begin();
set<int>::iterator itb = myset.end();
myset.erase(ita);//根据迭代器位置删除元素
printSet(++count);
ita = myset.begin();
itb = myset.begin();
itb++; itb++;
myset.erase(ita, itb); //删除区间[ita,itb)的元素
printSet(++count);
myset.clear();
system("pause");
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200709203718257.png)
修改
不能直接修改,需要删除后再次添加。
查找
s.find() 查找一个元素,如果容器中不存在该元素,返回值等于s.end()
#include <iostream>
#include <set>
#include <vector>
using namespace std;
set<int> myset;
void printSet(int count){
cout << "Test output" << count << ":" << endl;
for (set<int>::iterator it = myset.begin(); it != myset.end(); it++){
cout << *it << " ";
}
cout << endl;
}
int main(){
int count = 1;
for (int i = 1; i < 11; i++){
myset.insert(i);
}
if (myset.find(2) != myset.end())
cout << "2 is in the set" << endl;
else
cout << "2 is not in the set" << endl;
if (myset.find(20) == myset.end())
cout << "20 is not in the set" << endl;
else
cout << "20 is in the set" << endl;
myset.clear();
system("pause");
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200709210123555.png)
4.其它操作
s.lower_bound() 返回第一个大于或等于给定关键值的元素
s.upper_bound() 返回第一个大于给定关键值的元素
s.equal_range() 返回一对定位器,分别表示 第一个大于或等于给定关键值的元素 和 第一个大于给定关键值
的元素,这个返回值是一个pair类型,如果这一对定位器中哪个返回失败,就会等于
s.end()
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main(){
int count = 1;
for (int i = 1; i < 11; i++){
myset.insert(i);
}
cout << "lower_bound & upper_bound test:" << endl;
cout << "第一个大于等于3的元素:" << *myset.lower_bound(3) << endl; //函数实际返回地址值
cout << "第一个大于3的元素:" << *myset.upper_bound(3) << endl;
cout << "equal_range test:" << endl;
cout << "第一个大于或等于2的元素: " << *myset.equal_range(2).first << endl;
cout << "第一个大于2的元素: " << *myset.equal_range(2).second << endl;
if (myset.equal_range(20).first == myset.end())
cout << "不存在大于等于20的元素" << endl;
myset.clear();
system("pause");
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200709211332484.png)
判断为空 & 元素是否在set中
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main(){
int count = 1;
for (int i = 1; i < 11; i++){
myset.insert(i);
}
if (!myset.empty())
cout << "set is not empty" << endl;
if (myset.count(3))
cout << "3 is in the set" << endl;
myset.clear();
system("pause");
return 0;
}