Determine the lexicographical relationship between two maps : map compare « map multimap « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » map multimap » map compare 
23.3.3.Determine the lexicographical relationship between two maps
#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));

  // Declare an iterator to a map<string, int>.
  map<string, int>::iterator itr;

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

  if(m < m2cout << "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<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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.