zoukankan      html  css  js  c++  java
  • multiset多重集合容器(常用的使用方法总结)

      关于C++STL中multiset集合容器的学习,看别人的代码一百遍,不如自己动手写一遍。
      multiset多重集合容器和set集合容器的使用方法大多相同,不同的是multiset多重集合容器允许重复的元素键值插入。

      1 #include <set>
      2 #include <string> 
      3 #include <iostream>
      4 using namespace std;
      5 
      6 struct myComp{
      7     bool operator () (const string &a,const string &b){
      8         if(a.compare(b) == 1)
      9             return 1;
     10         return 0;
     11     }
     12 }; 
     13 
     14 struct STUDENT{
     15     string name;
     16     double score;
     17     //重载"<"运算符,自定义排列规则 
     18     bool operator < (const STUDENT &a) const{
     19         return a.score<score;//由大到小。如果由小到大,则符号为> 
     20     } 
     21 }; 
     22 
     23 void print(multiset<string> ms);
     24 void rprint(multiset<string> ms);
     25 
     26 int main()
     27 {
     28     //多重集合的创建与插入 
     29     multiset<string> ms;
     30     ms.insert("abc");
     31     ms.insert("123");
     32     ms.insert("111");
     33     ms.insert("aaa");
     34     ms.insert("123");
     35     cout<<"中序正向遍历:
    ";
     36     print(ms);
     37     cout<<"中序逆向遍历:
    ";
     38     rprint(ms);
     39     /*运行结果
     40     中序正向遍历:
     41     111
     42     123
     43     123
     44     aaa
     45     abc
     46     中序逆向遍历:
     47     abc
     48     aaa
     49     123
     50     123
     51     111
     52     */
     53     
     54     //元素的查找
     55     /*同set集合容器一样,使用find()查找元素,如果找到返回钙元素的迭代器位置(如果该元素存在重复,则返回第
     56     一个重复元素的迭代器位置),否则,返回end()迭代器*/
     57     multiset<string>:: iterator it;
     58     it=ms.find("123");
     59     if(it != ms.end()) cout<<"找到键值为123的元素
    ";
     60     it=ms.find("456");
     61     if(it == ms.end()) cout<<"没有找到键值为456的元素
    ";
     62     /*运行结果
     63     找到键值为123的元素
     64     没有找到键值为456的元素
     65     */
     66     
     67     //多重集合元素的删除
     68     cout<<"删除前:
    ";
     69     print(ms); 
     70     int n=ms.erase("123");//删除键值等于某个值得所有重复元素,并返回删除元素的个数
     71     cout<<"删除了"<<n<<"个元素
    ";
     72     cout<<"删除后:
    ";
     73     print(ms);
     74     /*运行结果
     75     删除前:
     76     111
     77     123
     78     123
     79     aaa
     80     abc
     81     删除了2个元素
     82     删除后:
     83     111
     84     aaa
     85     abc
     86     */ 
     87     
     88     ms.clear();
     89     if(ms.empty()) cout<<"清除后为:空
    ";
     90     /*运行结果 
     91     清除后为:空
     92     */
     93     
     94     /*同样在使用insert()可以采用自定义比较函数,默认从小到大*/
     95     //当元素类型是基本数据类型时,采用在主函数前重载"()"操作符
     96     multiset<string,myComp> mss; 
     97     mss.insert("abc");
     98     mss.insert("123");
     99     mss.insert("111");
    100     mss.insert("aaa");
    101     mss.insert("123");
    102     
    103     multiset<string>::iterator it1;
    104     for(it1= mss.begin(); it1 != mss.end(); it1 ++){
    105         cout<<*it1<<endl;
    106     }
    107     /*运行结果 
    108     abc
    109     aaa
    110     123
    111     123
    112     111
    113     */
    114     
    115     //如果元素是结构体,可以将比较函数写在结构体内,见main函数之前的定义方法 
    116     multiset<STUDENT> students;
    117     STUDENT someone;
    118     someone.name="Jack";
    119     someone.score=80.5;
    120     students.insert(someone);
    121     
    122     someone.name="Tomi";
    123     someone.score=57.5;
    124     students.insert(someone); 
    125     
    126     someone.name="Nacy";
    127     someone.score=60.5;
    128     students.insert(someone);
    129     
    130     someone.name="Nacy";
    131     someone.score=60.5;
    132     students.insert(someone);
    133     
    134     multiset<STUDENT>:: iterator it2;
    135     for(it2 = students.begin(); it2 != students.end(); it2 ++){
    136         cout<<(*it2).name<<":"<<(*it2).score<<endl;
    137     }
    138     /*运行结果 
    139     Jack:80.5
    140     Nacy:60.5
    141     Nacy:60.5
    142     Tomi:57.5
    143     */
    144     return 0;
    145 }
    146 
    147 void print(multiset<string> ms)
    148 {
    149     multiset<string>:: iterator it;
    150     for(it=ms.begin(); it != ms.end(); it ++){
    151         cout<<*it<<endl;
    152     }
    153 }
    154 
    155 void rprint(multiset<string> ms)
    156 {
    157     multiset<string>::reverse_iterator rit;
    158     for(rit=ms.rbegin(); rit != ms.rend(); rit++){
    159         cout<<*rit<<endl;
    160     }
    161 }
  • 相关阅读:
    Fedora 8 三维特效美化全攻略
    用C++编写简单绘图语言的语法分析器
    linux tar 的使用
    jquery基础
    hibernate4中主要的配置文件配置
    在对List集合进行remove()等操作重写equals()和hashCode()方法的必要性
    jquery基础2
    javascript时间格式化
    linux之shell编程shell基础
    html会移动的文字
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/8509909.html
Copyright © 2011-2022 走看看