Standard C++ Library User Guide and Tutorial
the values into the new map, which will order the values by prefix. Once the new map is created, it is
then printed.
typedef map<long, string, less<long> > sortedMap;
typedef sortedMap::value_type sorted_entry_type;
void telephoneDirectory::displayByPrefix()
{
 cout << "Display by prefix" << endl;
 sortedMap sortedData;
 friendMap::iterator itr;
 for (itr = database.begin(); itr != database.end(); itr++)
 sortedData.insert(sortedMap::value_type((*itr).second, 
 (*itr).first));
 for_each(sortedData.begin(), sortedData.end(),
 printSortedEntry);
}
The function used to print the sorted entries is the following:
void printSortedEntry (const sorted_entry_type & entry) 
 { cout << entry.first << ":" << entry.second << endl; }
Graphs
 Obtaining the Sample Program
A map whose elements are themselves maps are a natural representation for a directed graph. For
example, suppose we use strings to encode the names of cities, and we wish to construct a map where
the value associated with an edge is the distance between two connected cities. We could create such
a graph as follows:
typedef map<string, int> stringVector;
typedef map<string, stringVector> graph;
const string pendleton("Pendleton"); // define strings for 
 // city names
const string pensacola("Pensacola");
const string peoria("Peoria");
const string phoenix("Phoenix");
const string pierre("Pierre");
const string pittsburgh("Pittsburgh");
const string princeton("Princeton");
const string pueblo("Pueblo");
graph cityMap; // declare the graph that holds the map
cityMap[pendleton][phoenix] = 4; // add edges to the graph
cityMap[pendleton][pueblo] = 8;










