日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

set和multiset

發布時間:2025/4/5 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 set和multiset 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 set和multiset基本概念
    • 2 set和multiset的常用用法
      • 2.1 set/multiset對象的默認構造
      • 2.2 Set/multiset 對象的帶參構造函數
      • 2.3 set對象的拷貝構造與賦值
      • 2.4 set的插入和pair的用法
      • 2.5 set與迭代器
      • 2.6 set/multiset的大小
      • 2.7 set/multiset的刪除
      • 2.8 set/multiset的查找
    • 3 set和multiset中的排序方法

1 set和multiset基本概念

set/multiset容器概念:

  • set和multiset是一個集合容器,其中set所包含的元素是唯一的,集合中的元素按一定的順序排列。set采用紅黑樹變體的數據結構實現,紅黑樹屬于平衡二叉樹。在插入操作和刪除操作上比vector快。在n個數中查找目標數的效率是 log2^n。

紅黑樹定義:是每個節點都帶有顏色屬性(顏色為紅色或黑色)的自平衡二叉查找樹,滿足下列性質:

  • 節點是紅色或黑色;
  • 根節點是黑色;
  • 所有葉子節點都是黑色節點(NULL);
  • 每個紅色節點必須有兩個黑色的子節點。(從每個葉子到根的所有路徑上不能有兩個連續的紅色節點。)
  • 從任一節點到其每個葉子的所有簡單路徑都包含相同數目的黑色節點。

  • (圖中的45節點是有錯誤的,應該比55大)。

    Set和multiset 特點:

    • set中元素插入過程是按排序規則插入,所以不能指定插入位置。
    • set不可以直接存取元素。(不可以使用at.(pos)與[]操作符)。

    multiset與set的區別:

    • set支持唯一鍵值,每個元素值只能出現一次;而multiset中同一值可以出現多次。
    • 不可以直接修改set或multiset容器中的元素值,因為該類容器是自動排序的。如果希望修改一個元素值,必須先刪除原有的元素,再插入新的元素。

    頭文件 #include <set> 。

    set和multiset簡單使用示例:

    #include <set> #include <iostream> #include <functional> #include <algorithm> using namespace std;int main(void) {set<int> setInt;multiset<int> msetInt;multiset<int> msetInt1(msetInt.begin(), msetInt.end());for(int i=0; i<10; i++){msetInt.insert(100-i);}//set 不允許插入相同的元素,而multiset 是支持插入多個相同元素的.msetInt.insert(99);multiset<int>::iterator it = msetInt.begin();for( ; it!=msetInt.end(); it++){cout<<*it;cout<<" ";}cout<<endl;system("pause");return 0; }

    2 set和multiset的常用用法

    2.1 set/multiset對象的默認構造

    set<int> setInt; //一個存放int的set容器。 set<float> setFloat; //一個存放float的set容器。 set<string> setString; //一個存放string的set容器。 multiset<int> mulsetInt; //一個存放int的multi set容器。 multiset<float> multisetFloat; //一個存放float的multi set容器。 multiset<string> multisetString; //一個存放string的multi set容器。

    2.2 Set/multiset 對象的帶參構造函數

    set(beg,end); //將[beg, end)區間中的元素拷貝給本身。 set(const set &s); //拷貝構造函數。 multiset(beg,end); //將[beg, end)區間中的元素拷貝給本身。 multiset(const multiset &s); //拷貝構造函數。

    2.3 set對象的拷貝構造與賦值

    set(const set &st); //拷貝構造函數 set& operator=(const set &st); //重載等號操作符 set.swap(st); //交換兩個集合容器setIntA.insert(5); set<int> setIntA; setIntA.insert(1); setIntA.insert(2); setIntA.insert(3); setIntA.insert(4);set<int> setIntB(setIntA); //1 2 3 4 5set<int> setIntC; setIntC = setIntA; //1 2 3 4 5 setIntC.insert(6); //1 2 3 4 5 6 setIntC.swap(setIntA); //交換

    2.4 set的插入和pair的用法

    pair表示一個對組,它將兩個值視為一個單元,把兩個值捆綁在一起。
    pair<T1,T2>用來存放的兩個值的類型,可以不一樣,也可以一樣,如T1為int,T2為float。T1,T2也可以是自定義類。

    • pair.first是pair里面的第一個值,是T1類型。
    • pair.second是pair里面的第二個值,是T2類型。
    set<int> setInt; for(int i=5; i>0; i--){pair<set<int>::iterator, bool> ret = setInt.insert(i);if(ret.second){cout<<"插入 "<<i<<" 成功!"<<endl;}else {cout<<"插入 "<<i<<" 失敗!"<<endl;} }

    2.5 set與迭代器

    set.insert(elem); //在容器中插入元素。 set.begin(); //返回容器中第一個數據的迭代器。 set.end(); //返回容器中最后一個數據之后的迭代器。 set.rbegin(); //返回容器中倒數第一個元素的迭代器。 set.rend(); //返回容器中倒數最后一個元素的后面的迭代器。set<int> setInt; setInt.insert(3); setInt.insert(4); setInt.insert(1); setInt.insert(5); setInt.insert(2);//順序輸出 1 2 3 4 5 for(set<int>::iterator it=setInt.begin(); it!=setInt.end(); ++it) {int elem = *it;cout << elem; //或直接使用cout << *it }

    2.6 set/multiset的大小

    set.size(); //返回容器中元素的數目 set.empty();//判斷容器是否為空 //注意事項: 它們沒有resize 方法set<int> setIntA; setIntA.insert(3); setIntA.insert(1); setIntA.insert(7); setIntA.insert(5); setIntA.insert(9);if (!setIntA.empty()) {int iSize = setIntA.size(); //5 }

    2.7 set/multiset的刪除

    set.clear(); //清除所有元素 set.erase(pos); //刪除pos迭代器所指的元素,返回下一個元素的迭代器。 set.erase(beg,end); //刪除區間[beg,end)的所有元素,返回下一個元素的迭代器。 set.erase(elem); //刪除容器中值為elem的元素。//刪除區間內的某個或某些元素 //setInt是用set<int>聲明的容器,假設它內部現已包含按順序的1, 2, 3, 4, 5, 6元素。 set<int>::iterator itBegin=setInt.begin(); ++ itBegin; set<int>::iterator itEnd=setInt.begin(); ++ itEnd; ++ itEnd; ++ itEnd; setInt.erase(itBegin,itEnd); //此時容器setInt包含按順序的1, 4, 5, 6四個元素。//刪除容器中第一個元素 setInt.erase(setInt.begin()); //4, 5, 6//刪除容器中值為5的元素 setInt.erase(5); //4, 6//刪除setInt的所有元素 setInt.clear(); //容器為空

    2.8 set/multiset的查找

    set.find(elem); //查找elem元素,返回指向elem元素的迭代器。 set.count(elem); //返回容器中值為elem的元素個數。對set來說,要么是0,要么是1。對multiset來說,值可能大于1。 set.lower_bound(elem); //返回第一個>=elem元素的迭代器。 set.upper_bound(elem); // 返回第一個>elem元素的迭代器。 set.equal_range(elem); //返回容器中與elem相等的上下限的兩個迭代器。上限是閉區間,下限是開區間,如[beg,end)。以上函數返回兩個迭代器,而這兩個迭代器被封裝在pair中。set<int> setInt; setInt.insert(1); setInt.insert(2); setInt.insert(3); setInt.insert(4); setInt.insert(5);set<int>::iterator it1 = setInt.find(4); int elem1 = *it1; //elem1 == 4 int iCount = setInt.count(3); //iCount == 1set<int>::iterator it2 = setInt.lower_bound(3); set<int>::iterator it3 = setInt.upper_bound(3); int elem2 = *it2; //i2 == 3 int elem3 = *it3; //i3 == 4pair< set<int>::iterator, set<int>::iterator > pairIt = setInt.equal_range(5);

    3 set和multiset中的排序方法

    當我們往set或者multiset中插入的元素時,就會進行對元素之間進行比較,STL中是通過函數對象實現的,也被成為仿函數。使用函數對象而不是直接進行函數重載,是為了我們可以自定義比較方式,我們只需要定義自己的函數對象,就定義了完成了我們對排序方式的自定義。

    仿函數概念:

    • 盡管函數指針被廣泛用于實現函數回調,但C++還提供了一個重要的實現回調函數的方法,那就是函數對象。
    • functor,翻譯成函數對象,偽函數,它是是重載了“()”操作符的普通類對象。從語法上講,它與普通函數行為類似。
    • functional頭文件中包含的 greater<>與less<>就是函數對象。

    看一下set的模板聲明部分:

    下面舉出greater 和 less的簡易實現原理:

    struct greater { bool operator() (const int& iLeft, const int& iRight) const {return (iLeft>iRight); } }struct less { bool operator() (const int& iLeft, const int& iRight) const {return (iLeft<iRight); } }

    set/setmulti容器就是調用函數對象的operator()方法去比較兩個值的大小。

    下面給出示例程序:

    #include <set> #include <iostream> #include <functional> #include <algorithm>using namespace std;class student { public:student(int age) {this->age = age;}bool operator < (const student &right) const{return this->age > right.age;}int getAge() const { return age; }private:int age;string name;};class FunStudent{ public:bool operator () (const student &left, const student &right){cout<<"調用了 FunStudent ."<<endl;ret = left.getAge() < right.getAge();return ret;}public:int ret; };int main(void) {//less 函數對象實現比較,為排序提供依據//less 和greater 都是函數對象,有叫仿函數//set<int,less<int>> set1;set<int,greater<int>> set1;for(int i=5; i>0; i--){set1.insert(i);}//less<student>set<student, FunStudent> setStu; //等同于 set<student,less<student>>student lixiaohua(18);student wangdachui(19);//函數對象(仿函數)可以像函數一樣直接調用FunStudent funStu;funStu(lixiaohua, wangdachui);cout<<"比較結果:"<<funStu.ret<<endl;setStu.insert(lixiaohua);setStu.insert(wangdachui);for (set<student, FunStudent>::iterator it = setStu.begin(); it != setStu.end(); it++) {cout << it->getAge() ;cout << " ";}system("pause");return 0; }

    參考資料:

  • C/C++從入門到精通-高級程序員之路【奇牛學院】
  • 總結

    以上是生活随笔為你收集整理的set和multiset的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。