#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-09-18 18:09:26 发布
本文介绍了C++中set容器的使用,包括插入、查找、删除元素的方法,以及lower_bound和upper_bound的用法。通过实例展示了如何利用set进行元素管理和操作,并解释了如何利用find和count判断元素存在。
1226

被折叠的 条评论
为什么被折叠?



