STL set

STL 中的set容器可以存储某一类型的数据,并对数据按照规则自动排序,数据的值唯一,而且数据不能直接被改变。

2set的基本操作

1begin()  返回set中的第一个元素

2end()   返回set最后一个元素怒

3clear() 删除set中所有元素

4empty() 判断set是否为空

5max_size() 返回set容器中可能包含的元素最大个数

6size() 返回set中元素的个数

7: count()   返回某个元素的个数

8: find() 返回指定元素的迭代器

9: insert() 插入元素

10: swap() 交换两个元素

11: lower_bound() 返回>=某值的第一个元素的迭代器

12: upper_bound() 返回>某元素的迭代器

13: erase() 删除某元素

14:reverse_iterator  反向迭代器

插入和遍历

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 set<int>  s;
 4 int main()
 5 {
 6     for(int i=1;i<10;i++)
 7         s.insert(i*rand()%100);
 8     set<int>::iterator iter;
 9     for(iter=s.begin();iter!=s.end();iter++)
10         cout<<*iter<<" ";
11     cout<<endl;
12 ///sample output:  0 2 34 41 44 45 46 58 64
13     ///  insert another number  to see what happened
14     s.insert(23);
15     for(iter=s.begin();iter!=s.end();iter++)
16         cout<<*iter<<" ";
17     cout<<endl;
18 /// sample output: 0 2 23 34 41 44 45 46 58 64
19     /// it sorts element automatically
20     return 0;
21 }
View Code

find

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 set<int>s;
 4 void out(set<int> s)
 5 {
 6     auto iter=s.begin();
 7     for(;iter!=s.end();iter++)
 8         cout<<*iter<<" ";
 9     cout<<endl;
10 }
11 int main()
12 {
13     for(int i=1;i<10;i++)
14         s.insert(i*rand()%100);
15     out(s);
16     auto pos = s.find(23);
17     if(pos!=s.end())cout<<*pos<<endl;
18     else cout<<"did't exist"<<endl;
19     //find() return an iterator of the element
20     // if it doesn't exist  return end()
21     return 0;
22 }
View Code

比较

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 // create a cmp function to satisfy more requirement
 4 // two methods  1:a struct specially for cmp  2: overload <
 5 struct node
 6 {
 7     int age;
 8     string name;
 9     node(){}
10     node(int a1,string a2)
11     {
12         age=a1;name=a2;
13     }
14 };
15 /*
16 set 的泛型参数
17     template <class  T,class C, class A> class set;
18     第一个是元素类型 , 第二个是比较方式, 第三个是空间分配对象
19     第二个参数 如果使用默认值 元素必须有<
20     否则需要一个结构体重载()
21 */
22 // overload () !  that's wried
23 struct comp
24 {
25     bool operator()(const node&a,const node&b)
26     {
27         return a.name<b.name;
28     }
29 };
30 void out(set<node,comp>&s)
31 {
32     auto iter=s.begin();
33     for(;iter!=s.end();iter++)
34     {
35         cout<<iter->age<<"  "<<iter->name<<endl;;
36     }
37 }
38 int main()
39 {
40     set<node,comp>s;
41     s.insert(node(4,"xiaosha"));
42     s.insert(node(8,"zhuangzhuang"));
43     s.insert(node(19,"shuaishuainan"));
44     s.insert(node(29,"engineer"));
45     s.insert(node(70,"laotou"));
46     out(s);
47     return 0;
48 }
View Code

 删除

 1 //s.erase() 传入节点的迭代器,返回下一个节点的迭代器
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <set>
 5 #include <algorithm>
 6 using namespace std;
 7 int main()
 8 {
 9     set<int>s;
10     for(int i=100; i; i--)
11         s.insert(i);
12 
13     printf("%d\n\n",s.end());
14     set<int>::iterator it;
15     for (it=s.begin(); it!=s.end(); it++)
16     {
17         cout<<*it<<" ";
18     }
19     s.insert(10000);
20 
21     printf("%d\n\n",s.end());
22     cout<<"\n*********"<<endl;
23     int i=0;
24     for (it=s.begin(); it!=s.end(); )
25     {
26         i++;
27         if((*it)%3==0)
28             s.erase(it++);      //遍历时删除节点一定要将迭代器加一, 否则删除后 迭代器会出问题
29         else
30             it++;
31     }
32     cout<<"\n*********"<<endl;
33     for (it=s.begin(); it!=s.end(); it++)
34     {
35         cout<<*it<<" ";
36     }
37     return 0;
38 }
View Code

删除节点容易出错。

对于像list , map , set 等使用链接存储结构的容器 , 删除该节点后 要从这个迭代器访问下一个迭代器就不可行了。

而其他迭代器不失效  ; vector 是数组实现的 , 删除节点后,后面的节点会向前移 , 此时迭代器不必改变就已经是指向下一值了。

   //set<int>s;
1
for (it=s.begin(); it!=s.end(); ) 2 { 3 i++; 4 if((*it)%3==0) 5 s.erase(it++); 6 //遍历时删除节点一定要将迭代器加一, 否则删除后 迭代器会出问题 7 else 8 it++; 9 }
1 vector<int>::iterator it = v.begin();
2   for (it = v.begin(); it != v.end(); ) { 
3     if (*it % 2 == 0)
4       v.erase(it);//删除元素后,后面元素自动往前移,不用挪动指                  
5     else
6       ++it;
7   }

 

posted @ 2017-11-14 16:13  小螺号打豆豆  阅读(187)  评论(0编辑  收藏  举报