Mega Code Archive
Categories
/
C++ Tutorial
/
Map Multimap
Exchange the contents of two maps
#include
#include
#include
using namespace std; void show(const char *msg, map
mp); int main() { map
m, m2; m.insert(pair
("A", 100)); m.insert(pair
("G", 300)); m.insert(pair
("B", 200)); // Exchange the contents of m and m2. cout << "Exchange m and m2.\n"; m.swap(m2); show("Contents of m: ", m); show("Contents of m2: ", m2); // Clear m. m.clear(); if(m.empty()) cout << "m is now empty."; return 0; } // Display the contents of a map
by using an iterator. void show(const char *msg, map
mp) { map
::iterator itr; cout << msg << endl; for(itr=mp.begin(); itr != mp.end(); ++itr) cout << " " << itr->first << ", " << itr->second << endl; cout << endl; }