Graphs, Chapter 9 – HP Integrity NonStop H-Series User Manual
Page 104
![background image](/manuals/396950/104/background.png)
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
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
typedef map
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;