set的基本用法

set的含义是集合,其中的元素都是排好序的,支持插入、删除和查找,一定程度上可以替代平衡树,set和multiset的区别是:set插入的元素不能相同,但是multiset可以相同。

 

 

来自zz的模板题

 

代码如下:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<set>
 4 using namespace std;
 5 int n,m;
 6 set<int> s;
 7 void work1(){
 8     int num; scanf("%d",&num);
 9     if(s.find(num)==s.end()) s.insert(num);
10 }
11 void work2(){
12     int num; scanf("%d",&num);
13     if(s.find(num)!=s.end()) s.erase(num);
14 }
15 void work3(){
16     if(s.empty()){cout<<"-1"<<endl;return;}
17     cout<<*s.begin()<<endl;
18 }
19 void work4(){
20     if(s.empty()){cout<<"-1"<<endl;return;}
21     cout<<*(--s.end())<<endl;  
22 }
23 void work5(){
24     int num; scanf("%d",&num);
25     if(num==*s.begin()){cout<<"-1"<<endl;return;}
26     cout<<*(--s.lower_bound(num))<<endl;
27 }
28 void work6(){
29     int num; scanf("%d",&num);
30     if(s.find(num)==(--s.end())){cout<<"-1"<<endl;return;}
31     cout<<*s.upper_bound(num);
32 }
33 void work7(){
34     int num; scanf("%d",&num);
35     if(s.find(num)==s.end()){cout<<"-1"<<endl;return;}
36     cout<<"1"<<endl;
37 }
38 int main()
39 {
40     scanf("%d%d",&n,&m);
41     for(int i=1;i<=m;i++){
42         int num; scanf("%d",&num);
43         if(num==1) work1(); if(num==2) work2();
44         if(num==3) work3(); if(num==4) work4();
45         if(num==5) work5(); if(num==6) work6();
46         if(num==7) work7();
47     }
48     return 0;
49 }

 

posted @ 2017-09-02 20:15  Beginner_llg  阅读(451)  评论(0编辑  收藏  举报