set集合容器

 1 //set集合容器
 2 #include<set>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     set<int> s;
 9     //插入5个元素,但由于8有重复,第二次插入的8没有执行
10     s.insert(8);
11     s.insert(1); 
12     s.insert(12);
13     s.insert(6);
14     s.insert(8);
15     //中序遍历集合中的元素
16     set<int>::iterator it;
17     for(it=s.begin();it!=s.end();it++)
18     {
19         cout<<*it<<" ";
20      } 
21      cout<<endl;
22      //反向遍历集合中的元素 
23      set<int>::reverse_iterator rit;
24      for(rit=s.rbegin();rit!=s.rend();rit++)
25      {
26          cout<<*rit<<" ";
27      }
28      cout<<endl;
29      //删除键值为 6 的那个元素 
30      s.erase(6);
31      //查找键值为 6 的元素 
32      it=s.find(6);
33      if(it!=s.end())//找到
34          cout<<*it<<endl;
35     else//没找到 
36           cout<<"not find it"<<endl;
37     
38     
39     
40     //清空集合 
41     s.clear();
42     //输出集合的大小,为0
43     cout<<s.size()<<endl; 
44     return 0;
45 }

猜你喜欢

转载自www.cnblogs.com/zjl192628928/p/9161776.html