zoukankan      html  css  js  c++  java
  • wenbao与set/multiset

    set

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <set>
     5 using namespace std;
     6 
     7 //自定义比较函数mycmp,重载”()“操作符
     8 struct mycmp{
     9     bool operator()(const int &a,const int &b){
    10         return a>b;
    11     }
    12 };
    13 
    14 struct Info{
    15     string name;
    16     float score;
    17     //重载”<“操作符,自定义排序规则
    18     bool operator < (const Info &a) const {//虽然不知道为什么加const,但是必须要加
    19         //按score 由大到小排列。如果要由小到大排列,使用”>“号即可
    20         return a.score<score;
    21     }
    22 };
    23 
    24 int main(){
    25     set <int> s;
    26 
    27     //插入元素,自动排序,忽略重复的
    28     s.insert(1);
    29     s.insert(3);
    30     s.insert(2);
    31     s.insert(-1);
    32     s.insert(1);
    33     set<int>::iterator it;
    34     for(it=s.begin(); it!=s.end(); it++){
    35         cout<<*it<<" ";        
    36     }
    37 
    38     it=s.find(3);
    39     if(it!=s.end()){
    40         cout<<"YES"<<endl;
    41     }else{
    42         cout<<"NO"<<endl;        
    43     }
    44     
    45     //输出容器容量
    46     cout<<s.size()<<endl;
    47 
    48 
    49     //反向遍历集合中的元素
    50     set<int>::reverse_iterator rit;
    51     for(rit=s.rbegin(); rit!=s.rend(); rit++){
    52         cout<<*rit<<" ";
    53     }
    54 
    55     s.erase(3);
    56     for(rit=s.rbegin();rit!=s.rend();rit++){
    57         cout<<*rit<<" ";
    58     }
    59 
    60     s.clear();
    61     cout<<s.size()<<endl;
    62 
    63     set<int ,mycmp> ss;
    64     ss.insert(1);
    65     ss.insert(8);
    66     ss.insert(3);
    67     ss.insert(8);
    68     set<int,mycmp>::iterator myit;
    69     for(myit=ss.begin();myit!=ss.end();myit++){
    70         cout<<*myit<<" ";
    71     }
    72 
    73     set<Info>sss;
    74     Info info;
    75     info.name="Jack";
    76     info.score=80.5;
    77     sss.insert(info);
    78     info.name="Mike";
    79     info.score=99.5;
    80     sss.insert(info);
    81     info.name="Tom";
    82     info.score=45.9;
    83     sss.insert(info);
    84     set<Info>::iterator itt;
    85     for(itt=sss.begin();itt!=sss.end();itt++){
    86         cout<<(*itt).name<<" : "<<(*itt).score<<endl;
    87     }
    88     
    89     //lower_bound()
    90     set<int> s;
    91     cout<<*s.lower_bound(564);
    92     
    93     return 0;
    94 }

    @  输出(g++ std=c++11 x.cpp -o x)

    for(int i : s){
        cout<<i<<" ";
    }

    multiset

     1 #include <iostream>
     2 #include <string>
     3 #include <set>
     4 using namespace std;
     5 int main(){
     6     multiset<string> ms;
     7     ms.insert("abc");
     8     ms.insert("123");
     9     ms.insert("111");
    10     ms.insert("aaa");
    11     ms.insert("123");
    12     multiset<string>::iterator it;
    13     //for(it=ms.begin();it!=ms.end();it++)
    14     // cout<<*it<<" ";
    15     for(auto it:ms){
    16         cout<<it<<" ";    
    17     }
    18 
    19     //查找键值“123”,
    20     //若找到,返回迭代器位置(有多个重复则返回第一个)
    21     //若没有返回end();
    22     it=ms.find("123");
    23     if(it!=ms.end()){
    24         cout<<*it<<endl;
    25     }else{
    26         cout<<"No find it"<<endl;
    27     }
    28     it=ms.find("bbb");
    29     if(it!=ms.end()){
    30         cout<<*it<<endl;
    31     }else{
    32         cout<<"No find it"<<endl;
    33     }
    34 
    35     int n=ms.erase("123");
    36     cout<<n<<endl;
    37     for(auto it:ms){
    38         cout<<it<<" ";
    39     }
    40 
    41     return 0;
    42 }

    只有不断学习才能进步!

  • 相关阅读:
    css3中-moz、-ms、-webkit 是什么意思
    自定义AppServer
    自定义AppSession
    分离Command
    创建简单的Telnet实例
    注册表权限设置
    centos root登录password 忘记解决的方法
    ajaxFileUpload+struts2实现多文件上传
    计算机图形学(二)输出图元_6_OpenGL曲线函数_2_中点画圆算法
    linux命令的别名alias,unalias
  • 原文地址:https://www.cnblogs.com/wenbao/p/7428912.html
Copyright © 2011-2022 走看看