STL學習筆記-- multiset


multiset 多重集合容器

    與 set 集合容器一樣, multiset 多重容器也使用紅黑樹組織元素數據,只是 multiset 容器允許將重復的元素鍵值插入,而 set 容器則不允許。multiset 容器實現了 Sorted Associativate Container 、Simple Associative Container 和 Multiple Associative Container 概念的接口規范
    
    在用 multiset 的時候,同樣需要引用頭文件 "#include <set>"
    說得通俗點,multiset 是比 set 更復雜一點點的容器。下面不重復介紹 multiset 其他的概念和函數,直接來幾行代碼。關於multiset 的一些其他方面的概念,可以參考 set 容器。
    
    multiset 、map 和 multimap 對 pair , Functor 用得比較多。。。要仔細理解 pair 和 Functor 的用法。(后續文章會討論 map  multimap )

 

遍歷 multiset 容器元素
    
/*
    用前向迭代器將容器中的元素從小到大打印出來
*/
    
-------------------------------------------------------- 遍歷 multiset 容器元素
#pragma warning(disable:4786)
#include <set>
#include <iostream>
using namespace std;
int main()
{
    multiset<int> ms;
    ms.insert(10);
    ms.insert(13);
    ms.insert(11);
    ms.insert(19);
    ms.insert(13);
    ms.insert(19);
    ms.insert(19);
    // 打印數據
    multiset<int>::iterator i, iend;
    iend = ms.end();
    for (i=ms.begin(); i!=iend; ++i)
        cout << *i << ' ';
    cout << endl;

    return 0;
}

 

 

反向遍歷 multiset 容器
 1 /*
 2     使用反向迭代器,將容器中的元素,進行反向遍歷,最后打印出來
 3 */
 4 
 5 -------------------------------------------------------- 反向遍歷 multiset 容器
 6 #include <set>
 7 #include <iostream>
 8 using namespace std;
 9 int main()
10 {
11     multiset<int>  ms;
12     ms.insert(9);
13     ms.insert(5);
14     ms.insert(4);
15     ms.insert(3);
16     ms.insert(7);
17     ms.insert(8);
18     ms.insert(6);
19     ms.insert(10);
20     ms.insert(10);
21     ms.insert(10);
22     ms.insert(4);
23     ms.insert(4);
24     // 反向遍歷打印
25     multiset<int>::reverse_iterator ri, riend;
26     riend = ms.rend();
27     for (ri=ms.rbegin(); ri!=riend; ++ri)
28         cout << *ri << ' ';
29     cout << endl;
30 
31     return 0;
32 }

 

 

multiset 容器的元素搜索
 1 /*
 2     利用 multiset 容器的 find 和 equal_range 函數,搜索鍵值為 13 的元素
 3 */
 4 
 5 -------------------------------------------------------- multiset 容器的元素搜索
 6 #pragma warning(disable:4786)
 7 #include <set>
 8 #include <iostream>
 9 using namespace std;
10 int main()
11 {
12     multiset<int> ms;
13     ms.insert(10);
14     ms.insert(13);
15     ms.insert(12);
16     ms.insert(11);
17     ms.insert(19);
18     ms.insert(13);
19     ms.insert(16);
20     ms.insert(17);
21     ms.insert(13);
22     // 打印所有元素
23     multiset<int>::iterator i, iend;
24     iend  = ms.end();
25     for (i=ms.begin(); i!=iend; ++i)
26         cout << *i << ' ';
27     cout << endl;
28 
29     // find 搜索元素 19
30     int v = 19;
31     multiset<int>::iterator i_v = ms.find(v);
32     cout << *i_v << endl;
33 
34     // equal_range 搜索元素 13
35     v = 13;
36     pair<multiset<int>::iterator, multiset<int>::iterator>  p = ms.equal_range(v);
37     cout << "大於等於" << v << "的第一個元素 ( X >= k ) 為:" << *p.first << endl;
38     cout << "大於" << v << "的第一個元素( x > k ) 為:" << *p.second << endl;
39 
40     // 打印重復鍵值元素 13
41     multiset<int>::iterator  j;
42     cout << "鍵值為" << v << "的所有元素為:";
43     for (j=p.first; j!=p.second; ++j)
44         cout << *j << ' ';
45     cout << endl << endl;
46 
47     return 0;
48 }

 

multiset 的其他函數用法
 1 /*
 2     下面的示例程序以學生記錄為元素插入 multiset 容器,比較函數是以年齡作比較,利用 size 和 count 函數統計了元素個數和某個鍵值下的元素個數
 3 */
 4 
 5 -------------------------------------------------------- multiset 的其他函數用法
 6 #pragma warning(disable:4786)
 7 #include <set>
 8 #include <iostream>
 9 using namespace std;
10 // 學生結構體
11 struct Student
12 {
13     char* name;
14     int year;
15     char* addr;
16 };
17 // 比較函數
18 struct StudentLess
19 {
20     bool operator()(const Student& s1, const Student& s2) const
21     {
22         return s1.year < s2.year;  // 比較學生年齡
23     }
24 };
25 
26 int main()
27 {
28     Student stuArray[] = {
29         {"張三", 23, "北京"},
30         {"李四", 24, "浙江"},
31         {"王五", 25, "上海"},
32         {"何亮", 22, "武漢"},
33         {"何生亮", 23, "深圳"}
34     };
35     // 創建 multiset 對象 ms
36     multiset<Student, StudentLess>  ms(stuArray, stuArray + 5, StudentLess());
37     // 統計
38     cout << "學生人數:" << ms.size() << endl << endl;
39     cout << "年齡為21歲的學生人數:" << ms.count(stuArray[0]) << endl << endl;
40     // 打印元素
41     multiset<Student, StudentLess>::iterator i, iend;
42     iend = ms.end();
43     cout << "姓名    " << "年齡    " << "地址    \n";
44     for (i=ms.begin(); i!=iend; ++i)
45         cout << (*i).name << "    " << (*i).year << "    " << (*i).addr << endl;
46     cout << endl;
47 
48     return 0;
49 }
50 /*    從 count 函數輸出可看到,真正作為鍵值的是 Student 結構體中的 year 變量值,而不是一個 Student 元素。因此,雖然 count(stuArray[0]) 傳遞的是一個 stuArray[0] 元素,但並不是統計等於 stuArray[0] 的元素個數,而是統計等於 stuArray[0].year 的元素個數,即年齡為 21 歲的學生人數為 2。
51 
52 */

 

 

 

 

------------------- multiset 小結
    multiset 多重集合容器是一個可容納重復元素鍵值的有序關聯容器。與 set 容器一樣,使用紅黑樹作為容器的內部數據結構,元素的搜索操作都是具有對數級的算法時間復雜度。它的 find 和 equal_range 函數,可搜索出某一鍵值下的所有元素位置。
    
    multiset 缺點:和 set 一樣,如果 插入、刪除 操作頻繁了,multiset 就不適合。
    multiset 優點:相對於 set ,它能插入重復的元素。當然,它的檢索速度也是非常快的。

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM