Compare two maps : map compare « map multimap « C++ Tutorial






#include <iostream>
#include <string>
#include <map>

using namespace std;

void show(const char *msg, map<string, int> mp);

int main() {
  map<string, int> m;

  m.insert(pair<string, int>("A", 100));
  m.insert(pair<string, int>("G", 300));
  m.insert(pair<string, int>("B", 200));

  // Create another map that is the same as the first.
  map<string, int> m2(m);
  show("Contents of m2: ", m2);

  // Compare two maps.
  if(m == m2) cout << "m and m2 are equivalent.\n\n";

  return 0;
}

// Display the contents of a map<string, int> by using an iterator.
void show(const char *msg, map<string, int> mp) {
  map<string, int>::iterator itr;

  cout << msg << endl;
  for(itr=mp.begin(); itr != mp.end(); ++itr)
    cout << "  " << itr->first << ", " << itr->second << endl;
  cout << endl;
}








23.3.map compare
23.3.1.Map comparison
23.3.2.Compare two maps
23.3.3.Determine the lexicographical relationship between two maps
23.3.4.Create another map that is the same as the first
23.3.5.Map with comparator(functor)