Section 10.1. Preliminaries: the pair Type


10.1. Preliminaries: the pair Type

Before 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 pairs

A 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"); 

Table 10.2. Operations on pairs

pair<T1, T2> p1;

Create an empty pair with two elements of types T1 and T2. The elements are value-initialized (Section 3.3.1, p. 92).

pair<T1, T2> p1(v1, v2);

 
 

Create a pair with types T1 and T2 initializing the first member from v1 and the second from v2.

make_pair(v1, v2)

Creates a new pair from the values v1 and v2. The type of the pair is inferred from the types of v1 and v2.

p1 < p2

Less than between two pair objects. Less than is defined as dictionary ordering: Returns true if p1.first < p2.first or if !(p2.first < p1.first) && p1.second < p2.second.

p1 == p2

Two pairs are equal if their first and second members are respectively equal. Uses the underlying element == operator.

p.first

Returns the (public) data member of p named first.

p.second

Returns the (public) data member of p named second.


Operations on pairs

Unlike 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 pair

In 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...      } 

Exercises Section 10.1

Exercise 10.1:

Write a program to read a sequence of strings and ints, storing each into a pair. Store the pairs in a vector.

Exercise 10.2:

There are at least three ways to create the pairs in the program for the previous exercise. Write three versions of the program creating the pairs in each way. Indicate which form you think is easier to write and understand and why.




C++ Primer
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2006
Pages: 223
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net