Mega Code Archive
Categories
/
C++ Tutorial
/
Map Multimap
Determine the lexicographical relationship between two maps
#include
#include
#include
using namespace std; void show(const char *msg, map
mp); int main() { map
m; m.insert(pair
("A", 100)); m.insert(pair
("G", 300)); m.insert(pair
("B", 200)); // Declare an iterator to a map
. map
::iterator itr; // Create another map that is the same as the first. map
m2(m); show("Contents of m2: ", m2); if(m < m2) cout << "m is less than m2." << endl; // Remove the Beta from m. m.erase("Beta"); show("m after removing Beta: ", m); if(m > m2) cout << "Now, m is greater than m2." << endl; 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; }