STL學習筆記之容器--multiset
阿新 • • 發佈:2019-01-27
與 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 多重集合容器是一個可容納重複元素鍵值的有序關聯容器。與 set 容器一樣,使用紅黑樹作為容器的內部資料結構,元素的搜尋操作都是具有對數級的演算法時間複雜度。它的 find 和 equal_range 函式,可搜尋出某一鍵值下的所有元素位置。
multiset 缺點:和 set 一樣,如果 插入、刪除 操作頻繁了,multiset 就不適合。
在用 multiset 的時候,同樣需要引用標頭檔案 "#include <set>"
說得通俗點,multiset 是比 set 更復雜一點點的容器。下面不重複介紹 multiset 其他的概念和函式,直接來幾行程式碼。關於multiset 的一些其他方面的概念,可以參考 set 容器。
multiset 、map 和 multimap 對 pair , Functor 用得比較多。。。要仔細理解 pair 和 Functor 的用法。(後續文章會討論 map multimap )
遍歷 multiset 容器元素
反向遍歷 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 容器的元素搜尋/* 使用反向迭代器,將容器中的元素,進行反向遍歷,最後打印出來 */ -------------------------------------------------------- 反向遍歷 multiset 容器 #include <set> #include <iostream> using namespace std; int main() { multiset<int> ms; ms.insert(9); ms.insert(5); ms.insert(4); ms.insert(3); ms.insert(7); ms.insert(8); ms.insert(6); ms.insert(10); ms.insert(10); ms.insert(10); ms.insert(4); ms.insert(4); // 反向遍歷列印 multiset<int>::reverse_iterator ri, riend; riend = ms.rend(); for (ri=ms.rbegin(); ri!=riend; ++ri) cout << *ri << ' '; cout << endl; return 0; }
/*
利用 multiset 容器的 find 和 equal_range 函式,搜尋鍵值為 13 的元素
*/
-------------------------------------------------------- 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(12);
ms.insert(11);
ms.insert(19);
ms.insert(13);
ms.insert(16);
ms.insert(17);
ms.insert(13);
// 列印所有元素
multiset<int>::iterator i, iend;
iend = ms.end();
for (i=ms.begin(); i!=iend; ++i)
cout << *i << ' ';
cout << endl;
// find 搜尋元素 19
int v = 19;
multiset<int>::iterator i_v = ms.find(v);
cout << *i_v << endl;
// equal_range 搜尋元素 13
v = 13;
pair<multiset<int>::iterator, multiset<int>::iterator> p = ms.equal_range(v);
cout << "大於等於" << v << "的第一個元素 ( X >= k ) 為:" << *p.first << endl;
cout << "大於" << v << "的第一個元素( x > k ) 為:" << *p.second << endl;
// 列印重複鍵值元素 13
multiset<int>::iterator j;
cout << "鍵值為" << v << "的所有元素為:";
for (j=p.first; j!=p.second; ++j)
cout << *j << ' ';
cout << endl << endl;
return 0;
}
multiset 的其他函式用法
/*下面的示例程式以學生記錄為元素插入 multiset 容器,比較函式是以年齡作比較,利用 size 和 count 函式統計了元素個數和某個鍵值下的元素個數 */
-------------------------------------------------------- multiset 的其他函式用法
#pragma warning(disable:4786)
#include <set>
#include <iostream>
using namespace std;
// 學生結構體
struct Student
{
char* name;
int year;
char* addr;
};
// 比較函式
struct StudentLess
{
bool operator()(const Student& s1, const Student& s2) const
{
return s1.year < s2.year; // 比較學生年齡
}
};
int main()
{
Student stuArray[] = {
{"張三", 23, "北京"},
{"李四", 24, "浙江"},
{"王五", 25, "上海"},
{"何亮", 22, "武漢"},
{"何生亮", 23, "深圳"}
};
// 建立 multiset 物件 ms
multiset<Student, StudentLess> ms(stuArray, stuArray + 5, StudentLess());
// 統計
cout << "學生人數:" << ms.size() << endl << endl;
cout << "年齡為21歲的學生人數:" << ms.count(stuArray[0]) << endl << endl;
// 列印元素
multiset<Student, StudentLess>::iterator i, iend;
iend = ms.end();
cout << "姓名 " << "年齡 " << "地址 \n";
for (i=ms.begin(); i!=iend; ++i)
cout << (*i).name << " " << (*i).year << " " << (*i).addr << endl;
cout << endl;
return 0;
}
/* 從 count 函式輸出可看到,真正作為鍵值的是 Student 結構體中的 year 變數值,而不是一個 Student 元素。因此,雖然 count(stuArray[0]) 傳遞的是一個 stuArray[0] 元素,但並不是統計等於 stuArray[0] 的元素個數,而是統計等於 stuArray[0].year 的元素個數,即年齡為 21 歲的學生人數為 2。
*/
------------------- multiset 小結multiset 多重集合容器是一個可容納重複元素鍵值的有序關聯容器。與 set 容器一樣,使用紅黑樹作為容器的內部資料結構,元素的搜尋操作都是具有對數級的演算法時間複雜度。它的 find 和 equal_range 函式,可搜尋出某一鍵值下的所有元素位置。
multiset 缺點:和 set 一樣,如果 插入、刪除 操作頻繁了,multiset 就不適合。
multiset 優點:相對於 set ,它能插入重複的元素。當然,它的檢索速度也是非常快的。
原文連結:http://www.cnblogs.com/music-liang/archive/2013/04/06/3001978.html