std::multimap::iterator it;

for (it = range.first; it != range.second; ++it)

    cout <<  (*it).second    << endl;

Declarations like those just listed helped motivate the C++11 automatic type deduction feature, which allows you to simplify the code as follows:

auto range = codes.equal_range(718);

cout << "Cities with area code 718:\n";

for (auto it = range.first; it != range.second; ++it)

    cout <<  (*it).second    << endl;

Listing 16.14 demonstrates most of these techniques. It also uses typedef to simplify some of the code writing.

Listing 16.14. multmap.cpp

// multmap.cpp -- use a multimap

#include

#include

#include

#include

typedef int KeyType;

typedef std::pair Pair;

typedef std::multimap MapCode;

int main()

{

    using namespace std;

    MapCode codes;

    codes.insert(Pair(415, "San Francisco"));

    codes.insert(Pair(510, "Oakland"));

    codes.insert(Pair(718, "Brooklyn"));

    codes.insert(Pair(718, "Staten Island"));

    codes.insert(Pair(415, "San Rafael"));

    codes.insert(Pair(510, "Berkeley"));

    cout << "Number of cities with area code 415: "

         << codes.count(415) << endl;

    cout << "Number of cities with area code 718: "

         << codes.count(718) << endl;

    cout << "Number of cities with area code 510: "

         << codes.count(510) << endl;

    cout << "Area Code   City\n";

    MapCode::iterator it;

    for (it = codes.begin(); it != codes.end(); ++it)

        cout << "    " << (*it).first << "     "

            << (*it).second    << endl;

    pair range

         = codes.equal_range(718);

    cout << "Cities with area code 718:\n";

    for (it = range.first; it != range.second; ++it)

        cout <<  (*it).second    << endl;

    return 0;

}

Here is the output of the program in Listing 16.14:

Number of cities with area code 415: 2

Number of cities with area code 718: 2

Number of cities with area code 510: 2

Area Code   City

    415     San Francisco

    415     San Rafael

    510     Oakland

    510     Berkeley

    718     Brooklyn

    718     Staten Island

Cities with area code 718:

Brooklyn

Staten Island

Unordered Associative Containers (C++11)

An unordered associative container is yet another refinement of the container concept. Like an associative container, an unordered associative container associates a value with a key and uses the key to find the value. The underlying difference is that associative containers are based on tree structures, whereas unordered associative containers are based on another form of data structure called a hash table. The intent is to provide containers for which adding and deleting elements is relatively quick and for which there are efficient search algorithms. The four unordered associative containers are called unordered_set, unordered_multiset, unordered_map, and unordered_multimap. Appendix G looks a bit further at these additions.

Function Objects (a.k.a. Functors)

Many STL algorithms use function objects, also known as functors. A functor is any object that can be used with () in the manner of a function. This includes normal function names, pointers to functions, and class objects for which the () operator is overloaded—that is, classes for which the peculiar-looking function operator()() is defined. For example, you could define a class like this:

class Linear

{

private:

    double slope;

    double y0;

public:

    Linear(double sl_ = 1, double y_ = 0)

        : slope(sl_), y0(y_) {}

    double operator()(double x) {return y0 + slope * x; }

};

The overloaded () operator then allows you to use Linear objects like functions:

Linear f1;

Linear f2(2.5, 10.0);

double y1 = f1(12.5);   // right-hand side is f1.operator()(12.5)

double y2 = f2(0.4);

Here y1 is calculated using the expression 0 + 1 * 12.5, and y2 is calculated using the expression 10.0 + 2.5 * 0.4. In the expression y0 + slope * x, the values for y0 and slope come from the constructor for the object, and the value of x comes from the argument to operator()().

Remember the for_each function? It applied a specified function to each member of a range:

for_each(books.begin(), books.end(), ShowReview);

In general, the third argument could be a functor, not just a regular function. Actually, this raises a question: How do you declare the third argument? You can’t declare it as a function pointer because a function pointer specifies the argument type. Because a container can contain just about any type, you don’t know in advance what particular argument type should be used. The STL solves that problem by using templates. The for_each prototype looks like this:

template

Function for_each(InputIterator first, InputIterator last, Function f);

The ShowReview() prototype is this:

void ShowReview(const Review &);

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги