资料来源:官方文档
multiset是一个按照特定排序储存元素的容器,多个元素可以有相同的值。元素的值即为其本身的键值。multiset中的值无法修改,可插入删除。常用于实现二叉树。
定义一个multiset:
1 // constructing multisets
2 #include <iostream>
3 #include <set>
4 using namespace std;
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 multiset<int> first; //基本定义
15
16 int myints[]= {10,20,30,20,20};
17 multiset<int> second (myints,myints+5); // pointers used as iterators
18
19 multiset<int> third (second); // 复制
20
21 multiset<int> fourth (second.begin(), second.end()); // iterator ctor.
22
23 multiset<int,classcomp> fifth; // class as Compare
24
25 bool(*fn_pt)(int,int) = fncomp;
26 multiset<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare
27
28 return 0;
29 }
容器间赋值:
1 // assignment operator with multisets 2 #include <iostream> 3 #include <set> 4 5 int main () 6 { 7 int myints[]={ 19,81,36,36,19 }; 8 std::multiset<int> first (myints,myints+5); // multiset with 5 ints 9 std::multiset<int> second; // empty multiset 10 11 second=first; // now second contains the 5 ints 12 first=std::multiset<int>(); // 清空first 13 14 std::cout << "Size of first: " << first.size() << ' '; 15 std::cout << "Size of second: " << second.size() << ' '; 16 return 0; 17 }
输出为:
1 Size of first: 0 2 Size of second: 5
遍历容器:
int main () { int myints[] = {42,71,71,71,12}; multiset<int> mymultiset (myints,myints+5); multiset<int>::iterator it;//定义一个在multiset上的iterator cout << "mymultiset contains:"; for (multiset<int>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it) cout << ' ' << *it; cout << ' '; return 0; }
输出:
mymultiset contains: 12 42 71 71 71
插入操作:
int main () { multiset<int> mymultiset; multiset<int>::iterator it; // set some initial values: for (int i=1; i<=5; i++) mymultiset.insert(i*10); // 10 20 30 40 50 it=mymultiset.insert(25); it=mymultiset.insert (it,27); // max efficiency inserting it=mymultiset.insert (it,29); // max efficiency inserting it=mymultiset.insert (it,24); // no max efficiency inserting (24<29) int myints[]= {5,10,15}; mymultiset.insert (myints,myints+3); cout << "mymultiset contains:"; for (it=mymultiset.begin(); it!=mymultiset.end(); ++it) cout << ' ' << *it; cout << ' '; return 0; }
输出:
myset contains: 5 10 10 15 20 24 25 27 29 30 40 50
删除某个特定元素:
1 int main () 2 { 3 std::multiset<int> mymultiset; 4 std::multiset<int>::iterator it; 5 6 // insert some values: 7 mymultiset.insert (40); // 40 8 for (int i=1; i<7; i++) mymultiset.insert(i*10); // 10 20 30 40 40 50 60 9 10 it=mymultiset.begin(); 11 it++; // ^ 12 13 mymultiset.erase (it); // 10 30 40 40 50 60 14 15 mymultiset.erase (40); // 10 30 50 60 16 17 it=mymultiset.find (50); 18 mymultiset.erase ( it, mymultiset.end() ); // 10 30 19 20 cout << "mymultiset contains:"; 21 for (it=mymultiset.begin(); it!=mymultiset.end(); ++it) 22 cout << ' ' << *it; 23 cout << ' '; 24 25 return 0; 26 }
清空容器:
1 multiset<int> list; 2 list.clear();