#include <iostream>
#include <cstdlib>
// Just to demo syntax of map
// A map is used to keep a collection of keys each with an  associated values.

// for example we could associate a student id with a mark
// and the collection of these could be stored in a map
// such that we could retrieve a given studentid's mark
// in O(log  n) where n is number of entries in the map
using namespace std;
#include <map>


int main()
{
  map <string,int> results;   // each entry in map will be a string (the key == studentid) and an int (mark)
  map <string,int> ::iterator rp;  // an iterator to search through the map
  string stdid;
  int mark;
  cout << "enter student id and mark  - terminate with control-d" << endl;
  cout << " eg " <<endl;
  cout << " 111 6" << endl;
  cout << " 333 2 " << endl;
  cout << " control-d (unix) or control-z(windows)" << endl;
  while (cin >> stdid >> mark)
     results.insert(make_pair(stdid,mark));  // we combine the studentid and the mark into an entry
                                             // and insert into the map

  // now can display the whole thing - why? why not!
  
  for (rp = results.begin(); 
       rp != results.end(); rp++)
    cout << rp->first << ' ' << rp->second <<endl;

  // to retrieve a particular student's marks
  // can retrieve in O(log n) time where n is number
  // of entries in map
  cout << "enter studentid  ";
  cin.clear();  // clears previous end-of-file which we used to terminate data entry
  cin >> stdid;
  rp = results.find(stdid);  // set iterator to point to entry if found
  if (rp != results.end())
    cout << "mark for " << rp->first << " is " << rp->second << endl;
  else
    cout << "not present " << endl; // iterator beyond end of map means that given key wasn't found
  system("pause");
  return EXIT_SUCCESS;
  
}



