目錄ios
頭文件:spa
#include <set>.net
集合和多重集合的惟一區別:多重集合能夠包含重複元素,集合不能夠包含重複元素。code
構造方法blog
示例:排序
set<int> intSet; set<int,greater<int> > otherIntSet; multiset<string> stringMultiSet; multiset<string, greater<string> > otherStringMultiset;
操做:ip
示例代碼:get
#include <list> #include <iostream> #include <algorithm> #include <iterator> #include <set> #include <string> using namespace std; int main() { set<int> intSet; // 集合 升序 set<int,greater<int> > intSetA;// 集合 降序 set<int,greater<int> >::iterator intSetAIt;// 迭代器 ostream_iterator<int> screen(cout," ");// 輸出迭代器 // intSet 添加元素 intSet.insert(16); intSet.insert(8); intSet.insert(20); intSet.insert(3); cout << "intSet :" << endl; copy(intSet.begin(),intSet.end(),screen); cout<< endl; // intSetA 添加元素 intSetA.insert(36); intSetA.insert(84); intSetA.insert(30); intSetA.insert(39); intSetA.insert(59); intSetA.insert(238); intSetA.insert(156); intSetA.insert(156); //重複插入 cout << "intSetA :" << endl; copy(intSetA.begin(),intSetA.end(),screen); cout << endl; // 擦除元素 intSetAIt = intSetA.begin(); ++intSetAIt; ++intSetAIt; ++intSetAIt; intSetA.erase(intSetAIt); cout << "intSetA.erase:" << endl; copy(intSetA.begin(),intSetA.end(),screen); cout << endl; set<int,greater<int> >intSetB(intSetA); cout << "intSetB :" << endl; copy(intSetB.begin(),intSetB.end(),screen); cout << endl; intSetB.clear(); cout << "intSetB.clear :" << endl; copy(intSetB.begin(),intSetB.end(),screen); cout << endl; // 定義多重集合 multiset<string, greater<string> > namesMultiSet; multiset<string, greater<string> >::iterator iter; ostream_iterator<string> pScreen(cout," "); namesMultiSet.insert("Donny"); namesMultiSet.insert("Zippy"); namesMultiSet.insert("Goofy"); namesMultiSet.insert("Hungry"); namesMultiSet.insert("Goofy"); namesMultiSet.insert("Donny"); cout << "namesMultiSet:" << endl; copy(namesMultiSet.begin(),namesMultiSet.end(),pScreen); cout <<endl; return 0; }
intSet :
3 8 16 20
intSetA :
238 156 84 59 39 36 30
intSetA.erase:
238 156 84 39 36 30
intSetB :
238 156 84 39 36 30
intSetB.clear :
namesMultiSet:
Zippy Hungry Goofy Goofy Donny Donny
string