1. 程式人生 > 其它 >C++進階-3-5-set/multiset容器

C++進階-3-5-set/multiset容器

C++進階-3-5-set/multiset容器

  1 #include<iostream>
  2 #include<set>
  3 using namespace std;
  4 
  5 // set/multiset容器
  6 
  7 void printSet(set<int>& s) {
  8 
  9     for (set<int>::iterator it = s.begin(); it != s.end(); it++)
 10     {
 11         cout << *it << "
"; 12 } 13 cout << endl; 14 } 15 16 // 1.構造和賦值 17 void test01() { 18 19 set<int> s1; 20 21 // 插入資料,只有insert 22 s1.insert(10); 23 s1.insert(40); 24 s1.insert(30); 25 s1.insert(20); 26 s1.insert(30); 27 28 // 元素插入,自動排序,且不執行有重複資料 29 printSet(s1);
30 31 // 拷貝構造 32 set<int>s2(s1); 33 printSet(s2); 34 35 // 賦值 36 set<int>s3; 37 s3 = s2; 38 printSet(s3); 39 40 } 41 42 // 2.大小和交換 43 void test02() { 44 45 set<int> s1; 46 47 s1.insert(10); 48 s1.insert(30); 49 s1.insert(20);
50 s1.insert(40); 51 52 printSet(s1); 53 54 // 判斷是否為空 55 if (s1.empty()) { 56 cout << "s1為空" << endl; 57 } 58 else { 59 cout << "s1不為空" << endl; 60 cout << "s1的大小為:" << s1.size() << endl; 61 } 62 63 // 交換 64 set<int> s2; 65 66 s2.insert(100); 67 s2.insert(300); 68 s2.insert(200); 69 s2.insert(400); 70 71 cout << "交換前:" << endl; 72 printSet(s1); 73 printSet(s2); 74 75 cout << "交換後:" << endl; 76 s1.swap(s2); 77 printSet(s1); 78 printSet(s2); 79 80 } 81 82 // 3.插入和刪除 83 void test03() { 84 85 set<int> s1; 86 87 // 插入 88 s1.insert(10); 89 s1.insert(30); 90 s1.insert(20); 91 s1.insert(40); 92 93 // 遍歷 94 printSet(s1); 95 96 // 刪除 97 s1.erase(s1.begin()); 98 printSet(s1); 99 100 // 刪除過載版本 101 s1.erase(30); 102 printSet(s1); 103 104 // 清空 105 //s1.erase(s1.begin(), s1.end()) 106 s1.clear(); 107 printSet(s1); 108 109 } 110 111 // 4.查詢和統計 112 void test04() { 113 114 set<int> s1; 115 116 s1.insert(10); 117 s1.insert(30); 118 s1.insert(20); 119 s1.insert(40); 120 121 printSet(s1); 122 123 // 查詢 124 // 存在,返回該元素的迭代器,不存在,返回set.end(); 125 126 set<int>::iterator pos = s1.find(30); 127 if (pos != s1.end()) { 128 cout << "找到元素" << endl; 129 } 130 else 131 { 132 cout << "未找到元素" << endl; 133 } 134 135 // 統計 136 int num = s1.count(40); // 統計40的個數 137 cout << "num = " << num << endl; 138 // 對於set而言,統計結果,要麼是0, 要麼是1 139 140 } 141 142 // 5. set和multiset區別 143 void test05() { 144 145 set<int> s; 146 147 // set插入資料的同時還會返回插入的結果,表示插入是否成功 148 pair<set<int>::iterator, bool>set = s.insert(10); 149 150 if (set.second) { 151 cout << "第一次插入成功!" << endl; 152 } 153 else 154 { 155 cout << "第一次插入失敗!" << endl; 156 } 157 158 set = s.insert(10); 159 160 if (set.second) { 161 cout << "第二次插入成功!" << endl; 162 } 163 else 164 { 165 cout << "第二次插入失敗!" << endl; 166 } 167 168 169 // multiset允許插入重複值 170 multiset<int>ms; 171 ms.insert(10); 172 ms.insert(10); 173 ms.insert(10); 174 175 for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) { 176 cout << *it << " "; 177 } 178 cout << endl; 179 } 180 181 182 // 6.pair對組建立 183 void test06() { 184 185 // pair對組,成對出現的資料,利用對組可以返回兩個資料 186 187 // 第一種方式 188 pair<string, int>p(string("Tom"), 20); 189 cout << "姓名:" << p.first << " 年齡:" << p.second << endl; 190 191 // 第二種資料 192 pair<string, int>p2 = make_pair("Jerry", 30); 193 cout << "姓名:" << p2.first << " 年齡:" << p2.second << endl; 194 195 } 196 197 // 7.set容器排序 198 199 class MyCompare { 200 public: 201 // 仿函式,重寫(),類似於函式 202 bool operator()(int v1, int v2) { 203 return v1 > v2; 204 } 205 }; 206 207 void test07() { 208 209 // set排序是由小到大的升序 210 // 利用仿函式,可以改變排序規則 211 212 set<int> s1; 213 214 s1.insert(10); 215 s1.insert(30); 216 s1.insert(20); 217 s1.insert(40); 218 219 printSet(s1); 220 221 // 改為從大到小,在插入之前操作 222 223 set<int, MyCompare> s2; 224 225 s2.insert(10); 226 s2.insert(30); 227 s2.insert(20); 228 s2.insert(40); 229 230 for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) 231 { 232 cout << *it << " "; 233 } 234 cout << endl; 235 } 236 237 238 int main() { 239 240 // 1.構造和賦值 241 //test01(); 242 243 // 2.大小和交換 244 //test02(); 245 246 // 3.插入和刪除 247 //test03(); 248 249 // 4.查詢和統計 250 //test04(); 251 252 // 5. set和multiset區別 253 //test05(); 254 255 // 6.pair對組建立 256 //test06(); 257 258 // 7.set容器排序--內建資料型別 259 //test07(); 260 261 system("pause"); 262 263 return 0; 264 } 265 266 // 總結 267 // 268 // set/multiset容器 269 // 270 // 簡介:所有元素都會在插入時自動排序 271 // 272 // 本質:set/multiset屬於關聯式容器,底層結構是二叉樹實現 273 // 274 // set/multiset區別: 275 // 1.set不允許容器中有重複的元素 276 // 2.multiset允許容器中有重複的元素 277 // 3.set插入資料的同時還會返回插入的結果,表示插入是否成功 278 // 4.multiset不會檢測資料,因此可以插入重複資料 279 //