關於C++STL中multiset集合容器的學習,看別人的代碼一百遍,不如自己動手寫一遍。
multiset多重集合容器和set集合容器的使用方法大多相同,不同的是multiset多重集合容器允許重復的元素鍵值插入。
1 #include <set> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 6 struct myComp{ 7 bool operator () (const string &a,const string &b){ 8 if(a.compare(b) == 1) 9 return 1; 10 return 0; 11 } 12 }; 13 14 struct STUDENT{ 15 string name; 16 double score; 17 //重載"<"運算符,自定義排列規則 18 bool operator < (const STUDENT &a) const{ 19 return a.score<score;//由大到小。如果由小到大,則符號為> 20 } 21 }; 22 23 void print(multiset<string> ms); 24 void rprint(multiset<string> ms); 25 26 int main() 27 { 28 //多重集合的創建與插入 29 multiset<string> ms; 30 ms.insert("abc"); 31 ms.insert("123"); 32 ms.insert("111"); 33 ms.insert("aaa"); 34 ms.insert("123"); 35 cout<<"中序正向遍歷:\n"; 36 print(ms); 37 cout<<"中序逆向遍歷:\n"; 38 rprint(ms); 39 /*運行結果 40 中序正向遍歷: 41 111 42 123 43 123 44 aaa 45 abc 46 中序逆向遍歷: 47 abc 48 aaa 49 123 50 123 51 111 52 */ 53 54 //元素的查找 55 /*同set集合容器一樣,使用find()查找元素,如果找到返回鈣元素的迭代器位置(如果該元素存在重復,則返回第 56 一個重復元素的迭代器位置),否則,返回end()迭代器*/ 57 multiset<string>:: iterator it; 58 it=ms.find("123"); 59 if(it != ms.end()) cout<<"找到鍵值為123的元素\n"; 60 it=ms.find("456"); 61 if(it == ms.end()) cout<<"沒有找到鍵值為456的元素\n"; 62 /*運行結果 63 找到鍵值為123的元素 64 沒有找到鍵值為456的元素 65 */ 66 67 //多重集合元素的刪除 68 cout<<"刪除前:\n"; 69 print(ms); 70 int n=ms.erase("123");//刪除鍵值等於某個值得所有重復元素,並返回刪除元素的個數 71 cout<<"刪除了"<<n<<"個元素\n"; 72 cout<<"刪除后:\n"; 73 print(ms); 74 /*運行結果 75 刪除前: 76 111 77 123 78 123 79 aaa 80 abc 81 刪除了2個元素 82 刪除后: 83 111 84 aaa 85 abc 86 */ 87 88 ms.clear(); 89 if(ms.empty()) cout<<"清除后為:空\n"; 90 /*運行結果 91 清除后為:空 92 */ 93 94 /*同樣在使用insert()可以采用自定義比較函數,默認從小到大*/ 95 //當元素類型是基本數據類型時,采用在主函數前重載"()"操作符 96 multiset<string,myComp> mss; 97 mss.insert("abc"); 98 mss.insert("123"); 99 mss.insert("111"); 100 mss.insert("aaa"); 101 mss.insert("123"); 102 103 multiset<string>::iterator it1; 104 for(it1= mss.begin(); it1 != mss.end(); it1 ++){ 105 cout<<*it1<<endl; 106 } 107 /*運行結果 108 abc 109 aaa 110 123 111 123 112 111 113 */ 114 115 //如果元素是結構體,可以將比較函數寫在結構體內,見main函數之前的定義方法 116 multiset<STUDENT> students; 117 STUDENT someone; 118 someone.name="Jack"; 119 someone.score=80.5; 120 students.insert(someone); 121 122 someone.name="Tomi"; 123 someone.score=57.5; 124 students.insert(someone); 125 126 someone.name="Nacy"; 127 someone.score=60.5; 128 students.insert(someone); 129 130 someone.name="Nacy"; 131 someone.score=60.5; 132 students.insert(someone); 133 134 multiset<STUDENT>:: iterator it2; 135 for(it2 = students.begin(); it2 != students.end(); it2 ++){ 136 cout<<(*it2).name<<":"<<(*it2).score<<endl; 137 } 138 /*運行結果 139 Jack:80.5 140 Nacy:60.5 141 Nacy:60.5 142 Tomi:57.5 143 */ 144 return 0; 145 } 146 147 void print(multiset<string> ms) 148 { 149 multiset<string>:: iterator it; 150 for(it=ms.begin(); it != ms.end(); it ++){ 151 cout<<*it<<endl; 152 } 153 } 154 155 void rprint(multiset<string> ms) 156 { 157 multiset<string>::reverse_iterator rit; 158 for(rit=ms.rbegin(); rit != ms.rend(); rit++){ 159 cout<<*rit<<endl; 160 } 161 }