10.1. Preliminaries: the pair TypeBefore we look at the associative containers, we need to know about a simple companion library type named pair, which is defined in the utility header. Creating and Initializing pairsA pair holds two data values. Like the containers, pair is a template type. Unlike the containers we've seen so far, we must supply two type names when we create a pair: A pair holds two data members, each of which has the corresponding named type. There is no requirement that the two types be the same. pair<string, string> anon; // holds two strings pair<string, int> word_count; // holds a string and an int pair<string, vector<int> > line; // holds string and vector<int> When we create pair objects with no initializer, the default constructor value-initializes the members. Thus, anon is a pair of two empty strings, and line holds an empty string and an empty vector. The int value in word_count gets the value 0 and the string member is initialized to the empty string. We can also provide initializers for each member: pair<string, string> author("James", "Joyce"); creates a pair named author, in which each member has type string. The object named author is initialized to hold two strings with the values "James" and "Joyce". The pair type can be unwieldy to type, so when we wish to define a number of objects of the same pair type, it is convenient to use a typedef (Section 2.6, p. 61): typedef pair<string, string> Author; Author proust("Marcel", "Proust"); Author joyce("James", "Joyce");
Operations on pairsUnlike other library types, the pair class gives us direct access to its data members: Its members are public. These members are named first and second, respectively. We can access them using the normal dot operator (Section 1.5.2, p. 25) member access notation: string firstBook; // access and test the data members of the pair if (author.first == "James" && author.second == "Joyce") firstBook = "Stephen Hero"; The library defines only a limited number of operations on pairs, which are listed in Table 10.2 on the preceding page. Generating a New pairIn addition to the constructors, the library defines the make_pair function, which generates a new pair from its two arguments. We might use this function to make a new pair to assign to an existing pair: pair<string, string> next_auth; string first, last; while (cin >> first >> last) { // generate a pair from first and last next_auth = make_pair(first, last); // process next_auth... } This loop processes a sequence of authors. The call to make_pair generates a new pair from the names read in the while condition. It is equivalent to the somewhat more complicated // use pair constructor to make first and last into a pair next_auth = pair<string, string>(first, last); Because the data members of pair are public, we could read the inputeven more directly as pair<string, string> next_auth; // read directly into the members of next_auth while (cin >> next_auth.first >> next_auth.second) { // process next_auth... } ![]() |