elational databases, scientific apps, and Web-based systems often need a vector-like container whose index can be of any type, not necessarily int. Such containers are known as associative containers or maps. For instance, a directory service application can store private names as indexes and phone numbers as their associated values:
directory["Harry"]=8225687;//insert "Harry" and his #
iterator it=directory.find("Harry");//get Harry's phone #
Other applications of associative containers include DNS servers that map URLs to IP addresses, dictionaries, inventories, payrolls, and so on.
How do you implement associative containers whose index type isn't confined to int?
Use the <map>library to create and manipulate associative containers.
Pairs and Maps
In a recent 10 Minute Solution, I presented the notion of a tuple, which is a collection of elements of different types. What I didn't say is that the C++98 Standard Library already has a specialized tuple type called pair. A pair binds a key (known as the first element) with an associated value (known as the second element). For example:
#include <utility> //definition of pair
pair <string, string> prof_and_course("Jones", "Syntax");
pair <int, string> symbolic_const (0, "false");
The Standard Library also defines a helper function that facilitates the creation of a pair type:
make_pair(prof,course);//returns pair <string,string>