《C++ Primer第五版》读书笔记(9)--ASSOCIATIVE CONTAINERS(一)

2014-11-24 09:48:36 · 作者: · 浏览: 5
Elements in an associative container are stored and retrieved by a key. In contrast, elements in a sequential container are stored and accessed sequentially by their position in the container.
The two primary associative-container types are map and set.The library provides eight associative containers, listed in Table 11.1.
\
11.1 USING AN ASSOCIATIVE CONTAINER
The map type is often referred to as an associative array.
A set is most useful when we simply want to know whether a value is present.


Using a map:
#include
#include
#include vc3RyZWFtPjxicj4KdXNpbmcgbmFtZXNwYWNlIHN0ZDs8YnI+CmludCBtYWluKCk8YnI+Cns8YnI+CiAgICAgICAgLy8gY291bnQgdGhlIG51bWJlciBvZiB0aW1lcyBlYWNoIHdvcmQgb2NjdXJzIGluIHRoZSBpbnB1dDxicj4KICAgICAgICBtYXA8c3RyaW5nLCBzaXplX3Q+IHdvcmRfY291bnQ7IC8vIGVtcHR5IG1hcCBmcm9tIHN0cmluZyB0byBzaXplX3Q8YnI+CiAgICAgICAgc3RyaW5nIHdvcmQ7PGJyPgogICAgICAgIHdoaWxlIChjaW4gPj4gd29yZCk8YnI+CiAgICAgICAgICAgJiM0MzsmIzQzO3dvcmRfY291bnRbd29yZF07ICAvLyBmZXRjaCBhbmQgaW5jcmVtZW50IHRoZSBjb3VudGVyIGZvciB3b3JkPGJyPgogICAgICAgIGZvciAoY29uc3QgYXV0byAmYW1wO3cgOiB3b3JkX2NvdW50KSAvLyBmb3IgZWFjaCBlbGVtZW50IGluIHRoZSBtYXA8YnI+CiAgICAgICAgICAgICAgICAgICAgICAvLyBwcmludCB0aGUgcmVzdWx0czxicj4KICAgICAgICAgICAgICAgICAgICAgICAgY291dCA8PCAgdy5maXJzdCA8PCA=" occurs " << w.second<< ((w.second > 1) " times" : " time") << endl;
return 0;
}
Like the sequential containers, the associative containers are templates. To define a map, we must specify both the key and value types.


Using a set

A logical extension to our program is to ignore common words like “the,” “and,” “or,” and so on.
// count the number of times each word occurs in the input
map word_count; // empty map from string to size_t
set exclude = {"The", "But", "And", "Or", "An", "A","the", "but", "and", "or", "an", "a"};
string word;
while (cin >> word)
// count only words that are not in exclude
if (exclude.find(word) == exclude.end())

++word_count[word]; // fetch and increment the counter for word


11.2 OVERVIEW OF THE ASSOCIATIVE CONTAINERS


The associative containers do not support the sequential-container position-specific operations, such as push_front or back. Because the elements are stored based on their keys, these operations would be meaningless for the associative containers. Moreover, the associative containers do not support the constructors or insert operations that take an element value and a count.
In addition to the operations they share with the sequential containers, the associative containers provide some operations (Table 11.7) and type aliases (Table 11.3) that the sequential containers do not. In addition, the unordered containers provide operations for tuning their hash performance, which we’ll cover in §11.4.
The associative container iterators are bidirectional.


11.2.1 DEFINING AN ASSOCIATIVE CONTAINER

Each of the associative containers defines a default constructor, which creates an empty container of the specified type. We can also initialize an associative container as a copy of another container of the same type or from a range of values, so long as those values can be converted to the type of the container. Under the new standard, we can also list initialize the elements:


map word_count; // empty

// list initialization
set exclude = {"the", "but", "and", "or", "an", "a","The", "But", "And", "Or", "An","A"};
// three elements; authors maps last name to first
map authors = { {"Joyce", "James"},{