beautypg.com

Example programs, A telephone database – HP Integrity NonStop H-Series User Manual

Page 102

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Example Programs

We present three example programs that illustrate the use of maps and multimaps. These are a
telephone database, graphs, and a concordance.

A Telephone Database

Obtaining the Sample Program

A maintenance program for a simple telephone database is a good application for a map. The
database is simply an indexed structure, where the name of the person or business (a string) is the key
value, and the telephone number (a long) is the associated entry. We might write such a class as
follows:

typedef map > friendMap;
typedef friendMap::value_type entry_type;

class telephoneDirectory {
public:
void addEntry (string name, long number) // add new entry to
// database
{ database[name] = number; }

void remove (string name) // remove entry from database
{ database.erase(name); }

void update (string name, long number) // update entry
{ remove(name); addEntry(name, number); }

void displayDatabase() // display entire database
{ for_each(database.begin(), database.end(), printEntry); }

void displayPrefix(int); // display entries that match prefix

void displayByPrefix(); // display database sorted by prefix

private:
friendMap database;
};

This manual is related to the following products: