Online Book Reader

Home Category

Professional C__ - Marc Gregoire [208]

By Root 1372 0
stores its elements in a sorted, treelike, data structure.

The pair Utility Class

Before learning about the associative containers, you must become familiar with the pair class, which is defined in the header file. The pair is a class template that groups together two values of possibly different types. The values are accessible through the first and second public data members. operator== and operator< are defined for pairs to compare both the first and second elements. Here are some examples:

// Two-argument constructor and default constructor

pair myPair("hello", 5);

pair myOtherPair;

// Can assign directly to first and second

myOtherPair.first = "hello";

myOtherPair.second = 6;

// Copy constructor

pair myThirdPair(myOtherPair);

// operator<

if (myPair < myOtherPair) {

cout << "myPair is less than myOtherPair" << endl;

} else {

cout << "myPair is greater than or equal to myOtherPair" << endl;

}

// operator==

if (myOtherPair == myThirdPair) {

cout << "myOtherPair is equal to myThirdPair" << endl;

} else {

cout << "myOtherPair is not equal to myThirdPair" << endl;

}

Code snippet from Pair\PairTest.cpp

The output is as follows:

myPair is less than myOtherPair

myOtherPair is equal to myThirdPair

The library also provides a utility function template, make_pair(), that constructs a pair from two values. For example, you could use it like this:

pair aPair = make_pair(5, 10);

Code snippet from Pair\PairTest.cpp

Of course, in this case you could have just used the two-argument constructor. However, make_pair() is more useful when you want to pass a pair to a function. Unlike class templates, function templates can infer types from parameters, so you can use make_pair() to construct a pair without explicitly specifying the types. You can also use make_pair() in combination with the C++11 auto keyword as follows:

auto aSecondPair = make_pair(5, 10);

Code snippet from Pair\PairTest.cpp

Using plain old pointer types in pairs is risky because the pair copy constructor and assignment operator perform only shallow copies and assignments of pointer types. However, you can safely store smart pointers like shared_ptr in your pair.

map

The map is one of the most useful containers, defined in the header file. It stores key/value pairs instead of just a single value. Insertion, lookup, and deletion are all based on the key; the value is just “along for the ride.” The term “map” comes from the conceptual understanding that the container “maps” keys to values.

The map keeps elements in sorted order, based on the keys, so that insertion, deletion, and lookup all take logarithmic time. Because of the order, when you enumerate the elements, they come out in the ordering imposed by the type’s operator< or a user defined comparator. It is usually implemented as some form of balanced tree, such as a red-black tree. However, the tree structure is not exposed to the client.

You should use a map whenever you need to store and retrieve elements based on a “key” value and you would like to have them in a certain order.

Constructing maps

The map template takes four types: the key type, the value type, the comparison type, and the allocator type. As usual, we ignore the allocator in this chapter; see Chapter 17 for details. The comparison type is similar to the comparison type for priority_queue described earlier. It allows you to specify a different comparison class than the default. You usually shouldn’t need to change the sorting criteria. In this chapter, we use only the default less comparison. When using the default, make sure that your keys all respond to operator< appropriately.

If you’re interested in further detail, Chapter 13 explains how to write your own comparison classes.

If you ignore the comparison and allocator parameters (which we urge you to do), constructing a map is just like constructing a vector or list, except that you specify the key and value types separately in the template. For example, the following code constructs

Return Main Page Previous Page Next Page

®Online Book Reader