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

2014-11-24 09:48:36 · 作者: · 浏览: 1
"Austen", "Jane"},{"Dickens", "Charles"} };


Initializing a multimap or multiset

The keys in a map or a set must be unique; there can be only one element with a given key. The multimap and multiset containers have no such restriction; there can be several elements with the same key.


11.2.2 REQUIREMENTS ON KEY TYPE
For the ordered containers―map, multimap, set, and multiset―the key type must define a way to compare the elements. By default, the library uses the

we can also supply our own operation to use in place of the


Using a Comparison Function for the Key Type:
// bookstore can have several transactions with the same ISBN
// elements in bookstore will be in ISBN order
multiset decltype(compareIsbn)*> bookstore(compareIsbn);

we use decltype to specify the type of our operation, remembering that when we use decltype to form a function pointer, we must add a * to indicate that we’re using a pointer to the given function type.


11.2.3 THE PAIR TYPE

Pair is a template from which we generate specific types.


pair anon; // holds two strings
pair word_count; // holds a string and an size_t

pair > line; // holds string and vector


Elements in a map are pairs.
The library defines only a limited number of operations on pairs, which are listed in Table 11.2
\
Under the new standard we can list initialize the return value:
pair process(vector &v)
{
// process v
if (!v.empty())
return {v.back(), v.back().size()}; // list initialize
else
return pair (); // explicitly constructed return value

}


11.3 OPERATIONS ON ASSOCIATIVE CONTAINERS
In addition to the types listed in Table 9.2(p. 330), the associative containers define the types listed in Table 11.3.
\
set ::value_type v1; // v1 is a string
set ::key_type v2; // v2 is a string
map ::value_type v3; // v3 is a pair
map ::key_type v4; // v4 is a string

map ::mapped_type v5; // v5 is an int


11.3.1 ASSOCIATIVE CONTAINER ITERATORS
When we dereference an iterator, we get a reference to a value of the container’s value_type. In the case of map, the value_type is a pair in which first holds the const key and second holds the value:
// get an iterator to an element in word_count
auto map_it = word_count.begin();
// *map_it is a reference to a pair object
cout << map_it->first; // prints the key for this element
cout << " " << map_it->second; // prints the value of the element
map_it->first = "new key"; // error: key is const
++map_it->second; // ok: we can change the value through an iterator


It is essential to remember that the value_type of a map is a pair and that we can change the value but not the key member of that pair.


Iterators for sets Are const

Although the set types define both the iterator and const_iterator types, both types of iterators give us read-only access to the elements in the set. Just as we cannot change the key part of a map element, the keys in a set are also const.


Iterating across an Associative Container
// get an iterator positioned on the first element
auto map_it = word_count.cbegin();
// compare the current iterator to the off-the-end iterator
while (map_it != word_count.cend()) {
// dereference the iterator to print the element key--value pairs
cout << map_it->first << " occurs "<< map_it->second << " times" << endl;
++map_it; // increment the iterator to denote the next element
}
When we use an iterator to traverse a map, multimap, set, or multiset, the iterators yield elements in ascending key order.


Associative Containers and Algorithms

In general, we do not use the generic algorithms (Chapter 10) with the associative containers. The fact that the keys are c