C++的STL之set/multiset

集合

使用set或multiset之前,必須加入頭文件<set>

Set、multiset都是集合類,差別在與set中不允許有重複元素,multiset中允許有重複元素。


sets和multiset內部以平衡二叉樹實現



1.   常用函數

1)        構造函數和析構函數

set c:創建空集合,不包含任何元素

set c(op):以op爲排序準則,產生一個空的set

set c1(c2):複製c2中的元素到c1中

set c(const value_type *first, const value_type* last):複製[first, last)之間元素構成新集合

set c(const value_type *first, const value_type* last,op):以op爲排序準則,複製[first, last)之間元素構成新集合。

c.~set()銷燬所有元素,釋放內存

multiset mc:創建空集合,不包含任何元素

multiset mc(op):以op爲排序準則,產生一個空的set

multiset c1(c2):複製c2中的元素到c1中

multiset c(const value_type *first, const value_type* last):複製[first, last)之間元素構成新集合

multiset c(const value_type *first, const value_type* last,op):以op爲排序準則,複製[first, last)之間元素構成新集合。

c.~set()銷燬所有元素,釋放內存

[cpp] view plain copy
  1. // constructing sets  
  2. #include <iostream>  
  3. #include <set>  
  4.   
  5. bool fncomp (int lhs, int rhs) {return lhs<rhs;}  
  6.   
  7. struct classcomp {  
  8.   bool operator() (const int& lhs, const int& rhs) const  
  9.   {return lhs<rhs;}  
  10. };  
  11.   
  12. int main ()  
  13. {  
  14.   std::set<int> first;                           // empty set of ints  
  15.   
  16.   int myints[]= {10,20,30,40,50};  
  17.   std::set<int> second (myints,myints+5);        // range  
  18.   
  19.   std::set<int> third (second);                  // a copy of second  
  20.   
  21.   std::set<int> fourth (second.begin(), second.end());  // iterator ctor.  
  22.   
  23.   std::set<int,classcomp> fifth;                 // class as Compare  
  24.   
  25.   bool(*fn_pt)(int,int) = fncomp;  
  26.   std::set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare  
  27.   
  28.   return 0;  
  29. }  


2)        大小、判斷空函數

    int size() const:返回容器元素個數
    bool empty() const:判斷容器是否爲空,若返回true,表明容器已空


3)        增加、刪除函數

      pair<iterator,bool> insert( x):插入元素x

    iterator insert(iterator it,x):在迭代器it處插入元素x

    void insert(const value_type *first,const value_type *last):插入[first, last)之間元素

    iterator erase(iterator it):刪除迭代器指針it處元素

    iterator erase(iterator first,iterator last):刪除[first, last)之間元素

    size_type erase(const Key& key):刪除元素值等於key的元素

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <set>  
  3.   
  4. int main ()  
  5. {  
  6.   std::set<int> myset;  
  7.   std::set<int>::iterator it;  
  8.   std::pair<std::set<int>::iterator,bool> ret;  
  9.   
  10.   // set some initial values:  
  11.   for (int i=1; i<=5; ++i) myset.insert(i*10);    // set: 10 20 30 40 50  
  12.   
  13.   ret = myset.insert(20);               // no new element inserted  
  14.   
  15.   if (ret.second==false) it=ret.first;  // "it" now points to element 20  
  16.   
  17.   myset.insert (it,25);                 // max efficiency inserting  
  18.   myset.insert (it,24);                 // max efficiency inserting  
  19.   myset.insert (it,26);                 // no max efficiency inserting  
  20.   
  21.   int myints[]= {5,10,15};              // 10 already in set, not inserted  
  22.   myset.insert (myints,myints+3);  
  23.   
  24.   std::cout << "myset contains:";  
  25.   for (it=myset.begin(); it!=myset.end(); ++it)  
  26.     std::cout << ' ' << *it;  
  27.   std::cout << '\n';  
  28.   
  29.   return 0;  
  30. }  

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <set>  
  3.   
  4. int main ()  
  5. {  
  6.   std::set<int> myset;  
  7.   std::set<int>::iterator it;  
  8.   
  9.   // insert some values:  
  10.   for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90  
  11.   
  12.   it = myset.begin();  
  13.   ++it;                                         // "it" points now to 20  
  14.   
  15.   myset.erase (it);  
  16.   
  17.   myset.erase (40);  
  18.   
  19.   it = myset.find (60);  
  20.   myset.erase (it, myset.end());  
  21.   
  22.   std::cout << "myset contains:";  
  23.   for (it=myset.begin(); it!=myset.end(); ++it)  
  24.     std::cout << ' ' << *it;  
  25.   std::cout << '\n';  
  26.   
  27.   return 0;  
  28. }  


4)        遍歷函數

     iterator begin():返回首元素的迭代器指針

    iterator end():返回尾元素的迭代器指針
    reverse_iterator rbegin():返回尾元素的逆向迭代器指針
    reverse_iterator rend():返回首元素前一個位置的迭代器指針

     

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <set>  
  3.   
  4. int main ()  
  5. {  
  6.   int myints[] = {75,23,65,42,13};  
  7.   std::set<int> myset (myints,myints+5);  
  8.   
  9.   std::cout << "myset contains:";  
  10.   for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)  
  11.     std::cout << ' ' << *it;  
  12.   
  13.   std::cout << '\n';  
  14.   
  15.   return 0;  
  16. }  

5)        操作函數

       const_iterator lower_bound(const Key& key):返回容器中大於等於key的迭代器指針

    const_iterator upper_bound(const Key& key):返回容器中大於key的迭代器指針

    int count(const Key& key) const:返回容器中元素等於key的元素的個數
    pair<const_iterator,const_iterator> equal_range(const Key& key) const:返回容器中元素值等於key的迭代指針[first, last)
    const_iterator find(const Key& key) const:查找功能,返回元素值等於key的迭代器指針
    void swap(set& s):交換集合元素
    void swap(multiset& s):交換多集合元素  

函數原型:

[cpp] view plain copy
  1. #include <algorithm>  
  2.  pair<forward_iterator,forward_iterator> equal_range( forward_iterator first, forward_iterator last, const TYPE& val );  
  3.  pair<forward_iterator,forward_iterator> equal_range( forward_iterator first, forward_iterator last, const TYPE& val, CompFn comp );  

函數equal_range()返回first和last之間等於val的元素區間.返回值是一對迭代器。 

此函數假定first和last區間內的元素可以使用<操作符或者指定的comp執行比較操作.
equal_range()可以被認爲是lower_bound和upper_bound的結合,

pair中的第一個迭代器由lower_bound返回, 第二個則由upper_bound返回.

下面的代碼使用equal_range()探測一個有序的vector中的可以插入數字8的位置:

[cpp] view plain copy
  1. vector<int> nums;  
  2.  nums.push_back( -242 );  
  3.  nums.push_back( -1 );  
  4.  nums.push_back( 0 );  
  5.  nums.push_back( 5 );  
  6.  nums.push_back( 8 );  
  7.  nums.push_back( 8 );  
  8.  nums.push_back( 11 );  
  9.  pair<vector<int>::iterator, vector<int>::iterator> result;  
  10.  int new_val = 8;    
  11.  result = equal_range( nums.begin(), nums.end(), new_val );   
  12. cout<< "The first place that "   
  13.  << new_val  
  14.  << " could be inserted is before " << *result.first  
  15.  << ", and the last place that it could be inserted is before " << *result.second  
  16.  << endl;  
運行結果:

[cpp] view plain copy
  1. The first place that 8 could be inserted is before 8, and the last place that it could be inserted is before 11  



[cpp] view plain copy
  1. #include <iostream>  
  2. #include <set>  
  3.   
  4. int main ()  
  5. {  
  6.   std::set<int> myset;  
  7.   std::set<int>::iterator itlow,itup;  
  8.   
  9.   for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90  
  10.   
  11.   itlow=myset.lower_bound (30);                //       ^  
  12.   itup=myset.upper_bound (60);                 //                   ^  
  13.   
  14.   myset.erase(itlow,itup);                     // 10 20 70 80 90  
  15.   
  16.   std::cout << "myset contains:";  
  17.   for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)  
  18.     std::cout << ' ' << *it;  
  19.   std::cout << '\n';  
  20.   
  21.   return 0;  
  22. }  
[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <set>  
  4.   
  5. using namespace std;  
  6.   
  7. int main ()  
  8. {  
  9.     set<int> myset;  
  10.   
  11.     for (int i=1; i<=5; i++) myset.insert(i*10);   // myset: 10 20 30 40 50  
  12.   
  13.     pair<set<int>::const_iterator,set<int>::const_iterator> ret;  
  14.     ret = myset.equal_range(30);  
  15.   
  16.     cout << "the lower bound points to: " << *ret.first << '\n';  
  17.     cout << "the upper bound points to: " << *ret.second << '\n';  
  18.   
  19.     return 0;  
  20. }  

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include <set>  
  4.   
  5. using namespace std;  
  6.   
  7. int main ()  
  8. {  
  9.     int myints[]={12,75,10,32,20,25};  
  10.     set<int> first (myints,myints+3);     // 10,12,75  
  11.     set<int> second (myints+3,myints+6);  // 20,25,32  
  12.   
  13.     first.swap(second);  
  14.   
  15.     cout << "first contains:";  
  16.     for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)  
  17.         cout << ' ' << *it;  
  18.     cout << '\n';  
  19.   
  20.     cout << "second contains:";  
  21.     for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)  
  22.         cout << ' ' << *it;  
  23.     cout << '\n';  
  24.   
  25.     return 0;  
  26. }  


發佈了43 篇原創文章 · 獲贊 16 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章