zoukankan      html  css  js  c++  java
  • STL之set

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cstring>
      4 #include<cstdlib>
      5 #include<set>
      6 
      7 using namespace std;
      8 
      9 /*仿函数*/
     10 class mycompare{
     11 public:
     12     bool operator()(int v1,int v2){
     13         return v1>v2;
     14     }
     15 };
     16 
     17 //初始化
     18 void test01(){
     19 
     20 
     21     set<int,mycompare> s1;/*自动进行排序,默认从小到大*/
     22     s1.insert(7);
     23     s1.insert(2);
     24     s1.insert(4);
     25     s1.insert(5);
     26     s1.insert(1);
     27     for(set<int>::iterator it=s1.begin(); it!=s1.end(); it++ ){
     28         cout<<(*it)<<" ";
     29     }
     30     cout<<endl;
     31 
     32     //如何改变默认排序?
     33 
     34     /*赋值操作*/
     35     set<int> s2;
     36 //    s2=s1;
     37 
     38     //删除操作
     39     s1.erase(s1.begin());
     40     s1.erase(7);
     41 
     42 
     43     for(set<int>::iterator it=s1.begin(); it!=s1.end(); it++ ){
     44         cout<<(*it)<<" ";
     45     }
     46     cout<<endl;
     47 }
     48 
     49 void test02(){
     50     set<int> s1;/*自动进行排序,默认从小到大*/
     51     s1.insert(7);
     52     s1.insert(2);
     53     s1.insert(4);
     54     s1.insert(5);
     55     s1.insert(1);
     56     /*find()函数返回迭代器*/
     57     set<int>::iterator vec=s1.find(4);
     58     if(vec==s1.end()){
     59         cout<<"没有找到!"<<endl;
     60     }
     61     else{
     62         cout<<"vec:"<<(*vec)<<endl;
     63     }
     64 
     65     /*lower_bound()返回第一个大于等于的迭代器*/
     66     vec=s1.lower_bound(2);
     67     if(vec==s1.end()){
     68         cout<<"没有找到!"<<endl;
     69     }
     70     else{
     71         cout<<"vec:"<<(*vec)<<endl;
     72     }
     73     /*找到第一个大于key的值*/
     74     vec=s1.upper_bound(2);
     75     if(vec==s1.end()){
     76         cout<<"没有找到!"<<endl;
     77     }
     78     else{
     79         cout<<"vec:"<<(*vec)<<endl;
     80     }
     81     //equal_range 返回lower_bound和upper_bound值,运用pair接收
     82     pair<set<int>::iterator,set<int>::iterator> myvec=s1.equal_range(2);
     83     if(myvec.first==s1.end()){
     84         cout<<"没有找到!"<<endl;
     85     }
     86     else{
     87         cout<<"myvec:" <<*(myvec.first)<<endl;
     88     }
     89     if(myvec.second==s1.end()){
     90         cout<<"没有找到!"<<endl;
     91     }
     92     else{
     93         cout<<"myvec:"<<*(myvec.second)<<endl;
     94     }
     95 }
     96 
     97 //在set中放自定义类型
     98 class Person{
     99 public:
    100     int id;
    101     int age;
    102 public:
    103     Person(int age,int id):id(id),age(age){}
    104 };
    105 //自定义排序规则
    106 class flag{
    107 public:
    108     bool operator()(Person p1, Person p2){
    109         return p1.age<p2.age;
    110     }
    111 };
    112 void test03(){
    113     set<Person,flag> sp;//set需要排序,当放对象的时候,要提前给定排序规则
    114     Person p1(10,20),p2(30,40),p3(50,60);
    115     sp.insert(p1);
    116     sp.insert(p2);
    117     sp.insert(p3);
    118 
    119     Person p4(10,30);
    120     for(set<Person,mycompare>::iterator it =sp.begin(); it!=sp.end(); it++ ){
    121         cout<<(*it).age<<" "<<(*it).id<<endl;
    122     }
    123 
    124     //查找
    125     set<Person,mycompare>::iterator vec=sp.find(p4);
    126     if(vec==sp.end()){
    127         cout<<"没有找到!"<<endl;
    128     }
    129     else{
    130         cout<<"找到!"<<(*vec).age<<" "<<(*vec).id<<endl;
    131     }
    132 }
    133 
    134 
    135 int main(){
    136     test01();
    137     return 0;
    138 }
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    Cs Round#54 E Late Edges
    Cs Round#54 D Spanning Trees
    python装饰器的理解
    java序列化,二进制和数据流
    netty的理解
    CenterOS卸载和安装MYSQL
    oracle的一些问题
    tomcat优化方案(转)
    Selector
    Buffer
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/10325796.html
Copyright © 2011-2022 走看看