multiset 多重集合容器 C++容器 学习笔记

2014-11-24 09:38:56 · 作者: · 浏览: 0
multiset与set一样,也是使用红黑树来组织元素数据的,唯一不同的是,multiset允许重复的元素键值插入,而set则不允许;

multiset也需要声明头文件#include .由于它包含重复元素,所以,在插入元素,删除元素,查找元素上和set也有差别;


multiset元素的插入


下例程序插入了重复值123,最后中序遍历了multiset对象;
运行结果为:
111
123
123
aaa
abc

#include 
  
   
#include 
   
     using namespace std; int main() { //定义元素类型为string的多重集合对象s,当前没有任何元素; multiset 
    
      ms; ms.insert("abc"); ms.insert("123"); ms.insert("111"); ms.insert("aaa"); ms.insert("123"); multiset 
     
       :: iterator it; for(it=ms.begin(); it != ms.end(); it++) { cout << *it << endl; } return 0; } 
     
    
   
  

multiset 元素的删除

采用erase()方法可以删除multiset对象中的某个迭代器位置上的元素,某段迭代器区间中的元素,键值等于某个值的所有重复元素,并且返回删除元素的个数,采用clear()方法可以清空容器内的所有元素;
下例程序详细说明了erase()方法的使用方法:

运行结果为:
All elements before deleted :
111
123
123
aaa
abc
Total deleted : 2
All elements after deleted :
111
aaa
abc
0

#include 
  
   
#include 
   
     using namespace std; int main() { //定义元素类型为string的多重集合对象s,当前没有任何元素; multiset 
    
      ms; ms.insert("abc"); ms.insert("123"); ms.insert("111"); ms.insert("aaa"); ms.insert("123"); multiset 
     
       :: iterator it; //定义前向迭代器 cout << "All elements before deleted : " << endl; for(it=ms.begin(); it != ms.end(); it++) { cout << *it << endl; } //删除键值为“123”的所有重复元素,返回删除元素总数2; int n = ms.erase("123"); cout << "Total deleted : " << n << endl; cout << "All elements after deleted : " << endl; for(it=ms.begin(); it != ms.end(); it++) { cout << *it << endl; } ms.clear(); cout << ms.size() << endl; return 0; } 
     
    
   
  

multiset 查找元素

使用find()方法查找元素,如果找到,则返回该元素的迭代器位置(如果该元素存在重复,则返回第一个元素重复元素的迭代器位置),如果没有找到,则返回end()迭代器位置;

下例程序具体说明了find()方法的使用方法:

运行结果为:
123
Not find it!

#include 
  
   
#include 
   
     using namespace std; int main() { //定义元素类型为string的多重集合对象s,当前没有任何元素; multiset 
    
      ms; ms.insert("abc"); ms.insert("123"); ms.insert("111"); ms.insert("aaa"); ms.insert("123"); multiset 
     
       :: iterator it; //定义前向迭代器 //查找键值"123" it = ms.find("123"); if(it != ms.end()) { //找到 cout << *it << endl; }else { //没找到 cout << "Not find it!" << endl; } //查找键值"bbb" it = ms.find("bbb"); if(it != ms.end()) { //找到 cout << *it << endl; }else { //没找到 cout << "Not find it!" << endl; } return 0; } 
     
    
   
  

01串排序 -- 多重set容器 ZOJ 1204

//ZOJ 1204

将01串首先按长度排好序,长度相同时,安1的个数进行排序,1的个数相同时再按ASCII码值排序;
01串的长度不超过256个字符;
Sample Input:
6
10011111
00001101
1010101
1
0
1100

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; struct cmp { bool operator()(const string s1, const string s2) { if(s1.length() != s2.length()) return s1.length() < s2.length(); int c1 = count(s1.begin(), s1.end(), '1'); int c2 = count(s2.begin(), s2.end(), '1'); return c1 == c2   s1 < s2 : c1 < c2; } }; int main(int argc, char *argv[]) { multiset 
      
        ms; string s; while(cin >> s) { ms.insert(s); } multiset 
       
         :: iterator it; for(it=ms.begin(); it != ms.end(); it++) { cout << *it << endl; } return 0; }