1、結構
set和multiset會根據特定的排序原則將元素排序。兩者不同之處在於,multisets允許元素重復,而set不允許重復。
只要是assignable、copyable、comparable(根據某個排序准則)的型別T,都可以成為set或者multisets的元素。如果沒有特別的排序原則,采用默認的less,已operator < 對元素進行比較,以便完成排序。
排序准則必須遵守以下原則:
- 必須是“反對稱的”,對operator <而言,如果x < y為真,y<x為假。
- 必須是“可傳遞的”,對operator <而言,如果x<y為真,y<z為真,那么x<z也為真。
- 必須是“非自反的”,對operator<而言,x<x永遠為假。
2、能力
和所有的標准關聯容器類似,sets和multisets通常以平衡二叉樹完成。
自動排序的主要優點在於使二叉樹搜尋元素具有良好的性能,在其搜索函數算法具有對數復雜度。但是自動排序也造成了一個限制,不能直接改變元素值,因為這樣會打亂原有的順序,要改變元素的值,必須先刪除舊元素,再插入新元素。所以sets和multisets具有以下特點:
- 不提供直接用來存取元素的任何操作元素
- 通過迭代器進行元素的存取。
3、操作函數
3.1 構造、拷貝、析構
操作 |
效果 |
set c |
產生一個空的set/multiset,不含任何元素 |
set c(op) |
以op為排序准則,產生一個空的set/multiset |
set c1(c2) |
產生某個set/multiset的副本,所有元素都被拷貝 |
set c(beg,end) |
以區間[beg,end)內的所有元素產生一個set/multiset |
set c(beg,end, op) |
以op為排序准則,區間[beg,end)內的元素產生一個set/multiset |
c.~set() |
銷毀所有元素,釋放內存 |
set<Elem> |
產生一個set,以(operator <)為排序准則 |
set<Elem,0p> |
產生一個set,以op為排序准則 |
3.2 非變動性操作
操作 |
效果 |
c.size() |
返回當前的元素數量 |
c.empty () |
判斷大小是否為零,等同於0 == size(),效率更高 |
c.max_size() |
返回能容納的元素最大數量 |
c1 == c2 |
判斷c1是否等於c2 |
c1 != c2 |
判斷c1是否不等於c2(等同於!(c1==c2)) |
c1 < c2 |
判斷c1是否小於c2 |
c1 > c2 |
判斷c1是否大於c2 |
c1 <= c2 |
判斷c1是否小於等於c2(等同於!(c2<c1)) |
c1 >= c2 |
判斷c1是否大於等於c2 (等同於!(c1<c2)) |
3.3 特殊的搜尋函數
sets和multisets在元素快速搜尋方面做了優化設計,提供了特殊的搜尋函數,所以應優先使用這些搜尋函數,可獲得對數復雜度,而非STL的線性復雜度。比如在1000個元素搜尋,對數復雜度平均十次,而線性復雜度平均需要500次。
操作 |
效果 |
count (elem) |
返回元素值為elem的個數 |
find(elem) |
返回元素值為elem的第一個元素,如果沒有返回end() |
lower _bound(elem) |
返回元素值為elem的第一個可安插位置,也就是元素值 >= elem的第一個元素位置 |
upper _bound (elem) |
返回元素值為elem的最后一個可安插位置,也就是元素值 > elem 的第一個元素位置 |
equal_range (elem) |
返回elem可安插的第一個位置和最后一個位置,也就是元素值==elem的區間 |
3.4 賦值
操作 |
效果 |
c1 = c2 |
將c2的元素全部給c1 |
c1.swap(c2) |
將c1和c2 的元素互換 |
swap(c1,c2) |
同上,全局函數 |
3.5 迭代器相關函數
sets和multisets的迭代器是雙向迭代器,對迭代器操作而言,所有的元素都被視為常數,可以確保你不會人為改變元素值,從而打亂既定順序,所以無法調用變動性算法,如remove()。
操作 |
效果 |
c.begin() |
返回一個隨機存取迭代器,指向第一個元素 |
c.end() |
返回一個隨機存取迭代器,指向最后一個元素的下一個位置 |
c.rbegin() |
返回一個逆向迭代器,指向逆向迭代的第一個元素 |
c.rend() |
返回一個逆向迭代器,指向逆向迭代的最后一個元素的下一個位置 |
3.6 安插和刪除元素
必須保證參數有效,迭代器必須指向有效位置,序列起點不能位於終點之后,不能從空容器刪除元素。
操作 |
效果 |
c.insert(elem) |
插入一個elem副本,返回新元素位置,無論插入成功與否。 |
c.insert(pos, elem) |
安插一個elem元素副本,返回新元素位置,pos為收索起點,提升插入速度。 |
c.insert(beg,end) |
將區間[beg,end)所有的元素安插到c,無返回值。 |
c.erase(elem) |
刪除與elem相等的所有元素,返回被移除的元素個數。 |
c.erase(pos) |
移除迭代器pos所指位置元素,無返回值。 |
c.erase(beg,end) |
移除區間[beg,end)所有元素,無返回值。 |
c.clear() |
移除所有元素,將容器清空 |
4、示例代碼
4.1 set
// cont/set1.cpp #include <iostream> #include <set> using namespace std; int main() { /*type of the collection: *-no duplicates *-elements are integral values *-descending order */ typedef set<int,greater<int> > IntSet; IntSet coll1; // empty set container //insert elements in random order coll1.insert(4); coll1.insert(3); coll1.insert(5); coll1.insert(1); coll1.insert(6); coll1.insert(2); coll1.insert(5); //iterate over all elements and print them IntSet::iterator pos; for (pos = coll1.begin(); pos != coll1.end(); ++pos) { cout << *pos << ' '; } cout << endl; //insert 4 again and process return value pair<IntSet::iterator,bool> status = coll1.insert(4); if (status.second) { cout << "4 inserted as element " << distance (coll1.begin(),status. first) + 1 << endl; } else { cout << "4 already exists" << endl; } //assign elements to another set with ascending order set<int> coll2(coll1.begin(), coll1.end()); //print all elements of the copy copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl; //remove all elements up to element with value 3 coll2.erase (coll2.begin(), coll2.find(3)); //remove all elements with value 5 int num; num = coll2.erase (5); cout << num << " element(s) removed" << endl; //print all elements copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl; }
輸出:
6 5 4 3 2 1 4 already exists 1 2 3 4 5 6 1 element(s) removed 3 4 6
4.2 multiset
// cont/mset1.cpp #include <iostream> #include <set> using namespace std; int main() { /*type of the collection: *-duplicates allowed *-elements are integral values *-descending order */ typedef multiset<int,greater<int> > IntSet; IntSet coll1, // empty multiset container //insert elements in random order coll1.insert(4); coll1.insert(3); coll1.insert(5); coll1.insert(l); coll1.insert(6); coll1.insert(2); coll1.insert(5); //iterate over all elements and print them IntSet::iterator pos; for (pos = coll1.begin(); pos != coll1.end(); ++pos) { cout << *pos << ' '; } cout << endl; //insert 4 again and process return value IntSet::iterator ipos = coll1.insert(4); cout << "4 inserted as element " << distance (coll1.begin(),ipos) + 1 << endl; //assign elements to another multiset with ascending order multiset<int> coll2(coll1.begin(), coll1.end()); //print all elements of the copy copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl; //remove all elements up to element with value 3 coll2.erase (coll2.begin(), coll2.find(3)); //remove all elements with value 5 int num; num = coll2.erase (5); cout << num << " element(s) removed" << endl; //print all elements copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl; }
輸出:
6 5 5 4 3 2 1 4 inserted as element 5 1 2 3 4 4 5 5 6 2 element(s) removed 3 4 4 6