#include<bits/stdc++.h>
using namespace std;
int main(){
set<int> st1; set中不能有重复元素 插入重复元素会被忽略 且自动排序
for(int i=0;i<10;i++){
st1.insert(i);
}
cout<<"size:"<<st1.size()<<endl;
查找set中是否有元素 用find()/count
if(st1.find(1)!=st1.end()) cout<<"1 found"<<endl;
if(st1.count(1)) cout<<"1 found"<<endl;
if(st1.find(11)==st1.end()) cout<<"11 not found"<<endl;
删除元素
st1.erase(1);
lower_bound(x)返回>=x的最小的数的迭代器 upper_bound(x)返回>x的最小的数的迭代器 不存在返回end()
cout<<*st1.lower_bound(5)<<endl;
cout<<*st1.upper_bound(5)<<endl;
数据结构 set
最新推荐文章于 2024-08-07 22:21:20 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)