1. Map & multimap 的拷貝構造與賦值
map(const map &mp); //拷貝構造函數
map& operator=(const map &mp); //重載等號操作符
map.swap(mp); //交換兩個集合容器
拷貝構造代碼示例:
1 #include <iostream> 2 #include <map> 3 4 using namespace std; 5 6 int main() 7 { 8 map<int, string> mapStu1; //默認為升序,與 map<int, string, less<int>> mapStu1; 同效 9 10 mapStu1.insert(pair<int, string>(1, "內容A")); 11 mapStu1.insert(pair<int, string>(2, "內容B")); 12 mapStu1.insert(pair<int, string>(3, "內容C")); 13 mapStu1.insert(pair<int, string>(4, "內容D")); 14 15 map<int, string>mapStu2(mapStu1); //拷貝構造,此時 mapStu2 與 mapStu1 中元素一致 16 17 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍歷 18 { 19 cout << "mapStu1的第 " << it->first << "個參數為: " << it->second <<endl; 20 } 21 cout << endl; 22 for (map<int, string>::iterator it = mapStu2.begin(); it != mapStu2.end(); it++) //mapStu2的遍歷 23 { 24 cout << "mapStu2的第 " << it->first << "個參數為: " << it->second << endl; 25 } 26 27 return 0; 28 }
打印結果:
重載等號操作代碼示例:
1 #include <iostream> 2 #include <map> 3 4 using namespace std; 5 6 int main() 7 { 8 map<int, string> mapStu1; //默認為升序,與 map<int, string, less<int>> mapStu1; 同效 9 10 mapStu1.insert(pair<int, string>(1, "內容A")); 11 mapStu1.insert(pair<int, string>(2, "內容B")); 12 mapStu1.insert(pair<int, string>(3, "內容C")); 13 mapStu1.insert(pair<int, string>(4, "內容D")); 14 15 map<int, string>mapStu2; 16 /* 17 源代碼已經實現等號重載,可以直接使用等號賦值 18 map& operator=(const map& _Right){ 19 _Mybase::operator=(_Right); 20 return *this; 21 } 22 */ 23 mapStu2 = mapStu1; 24 25 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍歷 26 { 27 cout << "mapStu1的第 " << it->first << "個參數為: " << it->second <<endl; 28 } 29 cout << endl; 30 for (map<int, string>::iterator it = mapStu2.begin(); it != mapStu2.end(); it++) //mapStu2的遍歷 31 { 32 cout << "mapStu2的第 " << it->first << "個參數為: " << it->second << endl; 33 } 34 35 return 0; 36 }
打印結果:
交換容器內容代碼示例:
1 #include <iostream> 2 #include <map> 3 4 using namespace std; 5 6 int main() 7 { 8 map<int, string> mapStu1; 9 map<int, string> mapStu2; 10 11 mapStu1.insert(pair<int, string>(1, "內容A")); 12 mapStu1.insert(pair<int, string>(2, "內容B")); 13 mapStu1.insert(pair<int, string>(3, "內容C")); 14 mapStu1.insert(pair<int, string>(4, "內容D")); 15 16 mapStu2.insert(pair<int, string>(5, "內容E")); 17 mapStu2.insert(pair<int, string>(6, "內容F")); 18 mapStu2.insert(pair<int, string>(7, "內容G")); 19 mapStu2.insert(pair<int, string>(8, "內容H")); 20 21 cout << "交換前打印:" << endl; 22 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍歷 23 { 24 cout << "mapStu1的第 " << it->first << "個參數為: " << it->second <<endl; 25 } 26 cout << endl; 27 for (map<int, string>::iterator it = mapStu2.begin(); it != mapStu2.end(); it++) //mapStu2的遍歷 28 { 29 cout << "mapStu2的第 " << it->first << "個參數為: " << it->second << endl; 30 } 31 32 mapStu1.swap(mapStu2); 33 cout << endl << "交換后打印:" << endl; 34 for (map<int, string>::iterator it = mapStu1.begin(); it != mapStu1.end(); it++) //mapStu1的遍歷 35 { 36 cout << "mapStu1的第 " << it->first << "個參數為: " << it->second << endl; 37 } 38 cout << endl; 39 for (map<int, string>::iterator it = mapStu2.begin(); it != mapStu2.end(); it++) //mapStu2的遍歷 40 { 41 cout << "mapStu2的第 " << it->first << "個參數為: " << it->second << endl; 42 } 43 44 return 0; 45 }
打印結果:
========================================================================================================================