// map example
//
// - a map is an ordered associative container
// - no matter when you insert an item, it is ordered internally
// - therefore, if you use an iterate to traverse the map, it is ordered
// - while inserting an item, you will also associate a value with it
// - suppose you insert key 5, you can have an associated value A with it
// - see http://www.cppreference.com/cppmap.html

#include <map>
#include <iostream>

using namespace std;

int main()
{
  map<int,string> m; // create a map with integer keys and string values

  // array indice is the key
              // front <--> back
  m[5] = "A"; // (5,A)
  m[4] = "G"; // (4,G) (5,A)
  m[7] = "B"; // (4,G) (5,A) (7,B)
  m[9] = "D"; // (4,G) (5,A) (7,B) (9,D)
  m[2] = "E"; // (2,E) (4,G) (5,A) (7,B) (9,D)
  m[3] = "H"; // (2,E) (3,H) (4,G) (5,A) (7,B) (9,D)

  cout << endl;

  cout << "map m has " << m.size() << " elements" << endl;

  // note: set also has a reverse_iterator as well
  cout << "map m has following elements (in order):" << endl;
  for (map<int,string>::const_iterator mit = m.begin(); mit != m.end(); mit++) {
    // mit->first contains the key
    // mit->second contains the value
    cout << "[Key: " << mit->first << " / Value: " << mit->second << "]" << endl;
  }
  cout << endl;

  map<int,string>::const_iterator fmit0 = m.find(4); // try to find key 4
  if (fmit0 != m.end()) { // if key not found, m.end() is returned
    cout << "map m has key 4" << endl;
  } else {
    cout << "map does not have key 4" << endl;
  }

  map<int,string>::const_iterator fmit1 = m.find(8); // try to find key 8
  if (fmit1 != m.end()) { // if key not found, m.end() is returned
    cout << "map m has key 8" << endl;
  } else {
    cout << "map m does not have key 8" << endl;
  }

  return 0;
}
