pair Before introducing map , let us first quickly discuss pair , a template struct that stores two elements of mixed types. pair (defined in <utility> ) accepts two template arguments and is declared as pair<TypeOne, TypeTwo> . pair has two fields, named first and second , which store the values of the two elements of the pair; first is a variable of type TypeOne , second of type TypeTwo . You can create a pair explicitly using this syntax: pair<int, string> myPair; myPair.first = myInteger; myPair.second = myString; Alternatively, you can initialize the pair 's members in the constructor as follows: pair<int, string> myPair (myInteger, myString) ; In some instances, you will need to create a pair on-the-fly to pass as a parameter (especially to the map 's in- sert ). You can therefore use the make_pair function as follows: pair<int, string> myPair = make_pair(137, "string!") ;
Chapter 7: STL Containers, Part II - 95 - Interestingly, even though we didn't specify what type of pair to create, the make_pair function was able to deduce the type of the pair from the types of the elements. This has to do with how C++ handles function tem- plates and we'll explore this in more detail later. map The map is one of the STL's most useful containers. Like the CS106B/X Map , the STL map is an associative container which lets you query which value is associated with a given key. Unlike the CS106B/X Map , the STL map lets you choose what key type you want to use. That is, the STL map can map string s to int s, int s to string s, or even vector<int> s to deque<double> s. Also unlike the CS106B/X Map , the STL map stores its elements in sorted order. That is, if you iterate over the keys in a map<string, int> , you'll get all of the string keys back in alphabetical order. As a consequence, if you want to use nonstandard types as keys in a map – for example, your own custom struct s – you will need to provide a comparison function. Again, like set , providing this function is quite difficult and we will not discuss how until we cover operator overloading. To declare a map , use the following syntax: map<KeyType, ValueType> myMap; For example, to create a map similar to the CS106B/X Map<int> , you'd write map<string, int> , a mapping from string s to int s. Unfortunately, while the STL map is powerful, its syntax is considerably more complicated than that of other containers because its functions and iterators need to accept, return, and manipulate two values. The map 's iter- ators act as pointers to pair<const KeyType, ValueType> s, pair of an immutable key and a mutable value. For example, to iterate over a map<string, int> and print out all its key/value pairs, you can use the follow- ing loop: for(map<string, int>::iterator itr = myMap.begin(); itr != myMap.end(); ++itr) cout << itr->first << ": " << itr->second << endl; Note that the key is itr->first and the value is itr->second . While you cannot change the value of the key during iteration, * you can modify its associated value.
- C++ Programming, .........