set的用法

set集合:集合中没有重复的元素(输入相同的元素时只保留一个,重复插入无效),并且在默认的情况下对集合里的元素按升序排序。所以涉及到排序并且不能重复时,想一下可不可以用set。

0.size();//返回当前set容器中的元素的个数

1.创建set对象:创建set对象时,需要指定元素的类型

     set<int>s;

     set<string>s;

2.元素的插入与遍历

   采用insert()的方法把元素插入到集合中,

    

[html] view plain copy

  1. <span style="font-size:18px;">#include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<string>  
  5. #include<set>  
  6. int main()  
  7. {  
  8.     set<int>s;  
  9.     set<int>::iterator it;//迭代器  
  10.     s.insert(5);//第一次插入5,可以插入  
  11.     s.insert(1);  
  12.     s.insert(3);  
  13.     s.insert(5);//第二次插入5,重复元素,不会插入  
  14.     for(it=s.begin();it!=s.end();it++)  
  15.         printf("%d ",*it);  
  16.     printf("\n");  
  17. }  
  18. //运行结果1 3 5</span>  

 3.元素的反向遍历

   使用反向迭代器reverse_iterator可以反向遍历集合,需要用到rbegin(),rend();

    

[html] view plain copy

  1. <span style="font-size:18px;">#include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<string>  
  5. #include<set>  
  6. using namespace std;  
  7. int main()  
  8. {  
  9.     set<int>s;  
  10.     set<int>::reverse_iterator it;//反向迭代器  
  11.     s.insert(5);  
  12.     s.insert(1);  
  13.     s.insert(3);  
  14.     for(it=s.rbegin();it!=s.rend();it++)  
  15.         printf("%d ",*it);  
  16.     printf("\n");  
  17. }  
  18. //运行结果5 3 1</span>  

4.元素的删除

  1. erase(iterator)  ,删除定位器iterator指向的值

  myset.erase(myset.begin());  

[html] view plain copy

  1. <span style="font-size:18px;">#include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<string>  
  5. #include<set>  
  6. using namespace std;  
  7. int main()  
  8. {  
  9.     set<int>s;  
  10.     set<int>::iterator it;//反向迭代器  
  11.     s.insert(5);  
  12.     s.insert(1);  
  13.     s.insert(3);  
  14.     s.insert(7);  
  15.     s.erase(1);//删除键值为1的元素  
  16.     for(it=s.begin();it!=s.end();it++)  
  17.         printf("%d ",*it);  
  18.     printf("\n");</span>  

[html] view plain copy

  1. <span style="font-size:18px;">    s.clear();//清空  
  2. }  
  3. //运行结果3 5 7  
  4. </span>  

5.元素的检索

  使用find()方法对集合进行检索,如果找到查找的的键值,则返回该键值的迭代器位置;否则,返回集合最后一个元素后面的一个位置,即end()。

[html] view plain copy

  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<string>  
  5. #include<set>  
  6. using namespace std;  
  7. int main()  
  8. {  
  9.     set<int>s;  
  10.     set<int>::iterator it;//反向迭代器  
  11.     s.insert(5);  
  12.     s.insert(1);  
  13.     s.insert(3);  
  14.     it=s.find(3);  
  15.     if(it!=s.end())  
  16.         cout<<*it<<endl;  
  17.     else  
  18.         cout<<"not find it"<<endl;  
  19.     it=s.find(7);  
  20.     if(it!=s.end())  
  21.         cout<<*it<<endl;  
  22.     else  
  23.         cout<<"not find it"<<endl;  
  24. }  
  25. //运行结果3 not find end  

6.自定义比较函数

         使用insert将元素插入到集合中去的时候,集合会根据设定的比较函数奖该元素放到该放的节点上去。在定义集合的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值从小到大的顺序插入元素。但在很多情况下,需要自己编写比较函数。

编写比较函数有两种方法。

(1)如果元素不是结构体,那么可以编写比较函数。下面的程序比较规则为按键值从大到小的顺序插入到集合中。

[html] view plain copy

  1. #include<iostream>  
  2. #include<set>  
  3. using namespace std;  
  4. struct mycomp  
  5. { //自定义比较函数,重载“()”操作符  
  6.     bool operator() (const int &a, const int &b)  
  7.     {  
  8.         if(a != b)  
  9.             return a > b;  
  10.         else  
  11.             return a > b;  
  12.     }  
  13. };  
  14. int main()  
  15. {  
  16.     set<int, mycomp> s; //采用比较函数mycomp  
  17.     s.insert(5); //第一次插入5,可以插入  
  18.     s.insert(1);  
  19.     s.insert(6);  
  20.     s.insert(3);  
  21.     s.insert(5); //第二次插入5,重复元素,不会插入  
  22.     set<int,mycomp>::iterator it;  
  23.     for(it = s.begin(); it != s.end(); it++)  
  24.         cout << *it << " ";  
  25.     cout << endl;  
  26.     return 0;  
  27. }  
  28. /*  
  29. 运行结果:6 5 3 1    
  30. */  

(2)如果元素是结构体,那么可以直接把比较函数写在结构体内。

[html] view plain copy

  1. #include<iostream>  
  2. #include<set>  
  3. #include<string>  
  4. using namespace std;  
  5. struct Info  
  6. {  
  7.     string name;  
  8.     double score;  
  9.     bool operator < (const Info &a) const // 重载“<”操作符,自定义排序规则  
  10.     {  
  11.         //按score由大到小排序。如果要由小到大排序,使用“>”即可。  
  12.         return a.score < score;  
  13.     }  
  14. };  
  15. int main()  
  16. {  
  17.     set<Info> s;  
  18.     Info info;  
  19.   
  20.     //插入三个元素  
  21.     info.name = "Jack";  
  22.     info.score = 80;  
  23.     s.insert(info);  
  24.     info.name = "Tom";  
  25.     info.score = 99;  
  26.     s.insert(info);  
  27.     info.name = "Steaven";  
  28.     info.score = 60;  
  29.     s.insert(info);  
  30.   
  31.     set<Info>::iterator it;  
  32.     for(it = s.begin(); it != s.end(); it++)  
  33.         cout << (*it).name << " : " << (*it).score << endl;   
  34.     return 0;  
  35. }  
  36. /*  
  37. 运行结果:  
  38. Tom : 99  
  39. Jack : 80  
  40. Steaven : 60  */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值