set容器

stl 的set容器很好用,非常有必要掌握

//  ascending  red black tree
// cross  set_intersection difference set_difference union set_union

multiset<int>st;//多重集
s.begin();//返回集合第一个元素
s.end();
s.clear();
s.empty();
s.insert();
s.erase();
s.size();

//创建

set<int>s;
set<int,greater<int > >s2;//带大于比较器的集合(默认为小于)
int a[5]={1,2,3,4,5};
set<int > s(a,a+5);//用数组初始化
set<int > s(setc.begin(),setc.end());//用集合初始化
set<int > sete(sete);//拷贝

//插入

s.insert(_x);
s.insert(a,a+5);

//删除

s.erase(_x);
s.clear();


//修改
:删除后再添加

//查找

s.find(_x);//存在? 地址:s.end();

s.lower_bound(_x);
s.upper_bound();

//自定义比较函数
struct cmp{
    bool operator () (const int &a,const int &b){
        return a>b;
    }
}
set<int,cmp>s;


//指针
set<int>::iterator it;
auto it;
next(it)//it 的下一个
prev(it)//it 的上一个

 

posted @ 2020-10-12 10:37  yesuweiYYYY  阅读(81)  评论(0编辑  收藏  举报