Source of map18.cpp


  1: //map18.cpp
  2: //Uses a map to create a "phone directory".

  4: #include <iostream>
  5: #include <map>
  6: #include <string>
  7: using namespace std;

  9: //This is the "key" class for the key/value pairs.
 10: //Note the inline function definitions.
 11: class Name
 12: {
 13: public:
 14:     Name()         { nameString = ""; }
 15:     Name(string s) { nameString = s; }
 16:     string getName() const { return nameString; }
 17: private:
 18:     string nameString;
 19: };

 21: //Define operator< for Name objects.
 22: //Try removing this definition to see what error is generated.
 23: bool operator<(Name n1, Name n2)
 24: {
 25:     return n1.getName() < n2.getName();
 26: }

 28: //This is the "value" class for the key/value pairs.
 29: //Again note the inline function definitions.
 30: class PhoneNumber
 31: {
 32: public:
 33:     PhoneNumber()         { numberString = ""; }
 34:     PhoneNumber(string s) { numberString = s; }
 35:     string getNumber() const { return numberString; }
 36: private:
 37:     string numberString;
 38: };


 41: int main()
 42: {
 43:     cout << "\nThis program enters some names and telephone numbers "
 44:         "into a \"phone directory\".\nThen it asks the user for a "
 45:         "name and provides the corresponding phone number,\nif that "
 46:         "name exists in the directory. Otherwise, it reports that the "
 47:         "given name\nis not in the directory.";
 48:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 50:     //Create map and put some names and numbers into it:
 51:     map<Name, PhoneNumber> directory;
 52:     directory.insert(pair<Name, PhoneNumber>(Name("Tom"),
 53:         PhoneNumber("423-4533")));
 54:     directory.insert(pair<Name, PhoneNumber>(Name("Dick"),
 55:         PhoneNumber("423-9678")));
 56:     directory.insert(pair<Name, PhoneNumber>(Name("Harry"),
 57:         PhoneNumber("423-8195")));
 58:     directory.insert(pair<Name, PhoneNumber>(Name("Alice"),
 59:         PhoneNumber("423-0809")));

 61:     cout << "\nHere are the names in the directory: \n";
 62:     map<Name, PhoneNumber>::iterator p = directory.begin();
 63:     while (p != directory.end())
 64:     {
 65:         cout << p->first.getName() << "\t" << p->second.getNumber() << "\n";
 66:         ++p;
 67:     }

 69:     //Given a name, find number (if it exists):
 70:     string nameToFind;
 71:     cout << "\nEnter a name, or press Enter to quit: ";
 72:     getline(cin, nameToFind);
 73:     while (nameToFind != "")
 74:     {
 75:         p = directory.find(Name(nameToFind));
 76:         if(p != directory.end())
 77:             cout << "Phone number for " << nameToFind << ": "
 78:                 <<  p->second.getNumber() << "\n";
 79:         else
 80:             cout << "The name \"" << nameToFind
 81:                 << "\" is not in the directory.\n";
 82:         cout << "\nEnter another name, or press Enter to quit: ";
 83:         getline(cin, nameToFind);
 84:     }
 85:     cout << "\nProgram is now terminating.";
 86:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 87: }