zoukankan      html  css  js  c++  java
  • C++set 和 multiset的使用

    最后一个自由支配的暑假,学一些自己感兴趣的部分,也算为大三作准备。

    C++中set集合的使用

    定义一个int类型的集合

    set<int> s;

    set<int>::iterator it;

    基本操作有如下:

    s.inert(10);//插入元素10

    s.erase(10);//删除元素10

    s.clear();//清空集合

    s.size();//集合元素的个数

    s.empty();//判断集合是否为空

    it=s.find(10);//查找集合中是否有元素10,有的话返回10,没有返回s.end();

    首先集合在数学的概念中就是互异性,不能有重复

    需要注意的点:

    1.是以中序遍历去遍历整个集合的,在插入的时候自动调整

    2.遍历的时候需要判断一下集合是否为空;

    3.插入的数默认从小到大排序 set<int>::iterator it; 

    4.如果要从大到小的话 set<int>::reverse_iterator rit;

             for(rit=s.rbegin();rit!=s.rend();rit++)

             {

                      cout<<*rit<<" ";

             }

     

     

    自定义比较函数,用到结构体

     1 #include<set>
     2 
     3 #include<string>
     4 
     5 #include<iostream>
     6 
     7 using namespace std;
     8 
     9  
    10 
    11 struct Info
    12 
    13 {
    14 
    15          string name;
    16 
    17          float score;
    18 
    19          //重载 '<'操作符
    20 
    21          bool operator < (const Info &a)const
    22 
    23          {
    24 
    25                   //按照score从小到大排序 换为‘<’则为从大到小
    26 
    27                   return a.score>score;
    28 
    29          }
    30 
    31 };
    32 
    33 int main()
    34 
    35 {
    36 
    37          set<Info> s;
    38 
    39          Info info;
    40 
    41         
    42 
    43          info.name="Jack";
    44 
    45          info.score=99;
    46 
    47          s.insert(info);
    48 
    49         
    50 
    51          info.name="Tom";
    52 
    53          info.score=56;
    54 
    55          s.insert(info);
    56 
    57         
    58 
    59          info.name="Nacy";
    60 
    61          info.score=85;
    62 
    63          s.insert(info);
    64 
    65         
    66 
    67          info.name="Elano";
    68 
    69          info.score=100;
    70 
    71          s.insert(info);
    72 
    73         
    74 
    75          set<Info>::iterator it;
    76 
    77          for(it=s.begin();it!=s.end();it++)
    78 
    79          cout<<(*it).name<<" : "<<(*it).score<<endl;
    80 
    81          return 0;
    82 
    83  
    84 
    85 }

    结果:

    mutiset:多重集合 和set最大的区别就是,它可以插入重复的元素,

    如果删除的话,相同的也一起删除了;

    如果查找的话,返回该元素的迭代器的位置,若有相同,返回第一个元素的地址;

    其他使用和set基本类似。

    #include<set>
    #include<string>
    #include<iostream>
    using namespace std;
    int main()
    {
        //多重集合对象 
        multiset<string> ms;
        ms.insert("abc");
        ms.insert("123");
        ms.insert("111") ;
        ms.insert("aaa");
        ms.insert("123"); 
        ms.insert("bbb");
        
        multiset<string>::iterator it;
        for(it=ms.begin();it!=ms.end();it++)
        {
            cout<<*it<<endl;
        }
        cout<<endl<<"集合的大小:"<<ms.size()<<endl;
        
        it=ms.find("123");
        if(it!=ms.end())
        {
            cout<<*it<<endl;
        }
        else cout<<"not found"<<endl;
        
        it=ms.find("43");
        if(it!=ms.end())
        {
            cout<<*it<<endl;
        }
        else cout<<"not found"<<endl;
        
        int n=ms.erase("123");
        cout<<"共删除:"<<n<<endl<<endl;
        for(it=ms.begin();it!=ms.end();it++)
        {
            cout<<*it<<endl;
        }
        return 0;    
    } 

     

     

     

  • 相关阅读:
    个人工作总结2
    个人工作总结1
    《梦断代码》阅读笔记01
    构建之法阅读笔记4
    四则运算-安卓版
    最大联通子数组之和(dfs,记忆化搜索,状态压缩)
    第五周进度报告
    第四周进度报告
    最大子数列之和问题
    构建之法阅读笔记3
  • 原文地址:https://www.cnblogs.com/tvtaqa/p/7128584.html
Copyright © 2011-2022 走看看