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

2014-11-24 09:48:36 · 作者: · 浏览: 2
onst means that we cannot pass associative container iterators to algorithms that write to or reorder container elements. Associative containers can be used with the algorithms that read elements. However, many of these algorithms search the sequence. Because elements in an associative container can be found (quickly) by their key, it is almost always a bad idea to use a generic search algorithm. For example, as we’ll see in § 11.3.5(p. 436), the associative containers define a member named find, which directly fetches the element with a given key. We could use the generic find algorithm to look for an element, but that algorithm does a sequential search. It is much faster to use the find member defined by the container than to call the generic version.

In practice, if we do so at all, we use an associative container with the algorithms either as the source sequence or as a destination. For example, we might use the generic copy algorithm to copy the elements from an associative container into another sequence. Similarly, we can call inserter to bind an insert iterator to an associative container. Using inserter, we can use the associative container as a destination for another algorithm.


11.3.2 ADDING ELEMENTS

The insert members (Table 11.4(overleaf)) add one element or a range of elements. Because map and set(and the corresponding unordered types) contain unique keys, inserting an element that is already present has no effect:


vector ivec = {2,4,6,8,2,4,6,8}; // ivec has eight elements
set set2; // empty set
set2.insert(ivec.cbegin(), ivec.cend()); // set2 has four elements
set2.insert({1,3,5,7,1,3,5,7}); // set2 now has eight elements
\
Adding Elements to a map:
// four ways to add word to word_count
word_count.insert({word, 1});
word_count.insert(make_pair(word, 1));
word_count.insert(pair (word, 1));

word_count.insert(map ::value_type(word, 1));


Testing the Return from insert:


For the containers that have unique keys, the versions of insert and emplace that add a single element return a pair that lets us know whether the insertion happened. The first member of the pair is an iterator to the element with the given key; the second is a bool indicating whether that element was inserted, or was already there. If the key is already in the container, then insert does nothing, and the bool portion of the return value is false. If the key isn’t present, then the element is inserted and the bool is true.
// more verbose way to count number of times each word occurs in the input
map word_count; // empty map from string to size_t
string word;
while (cin >> word) {
// inserts an element with key equal to word and value 1;
// if word is already in word_count, insert does nothing
auto ret = word_count.insert({word, 1});
if (!ret.second) // word was already in word_count
++ret.first->second; // increment the counter

}


Adding Elements to multiset or multimap


Because keys in a multi container need not be unique, insert on these types always inserts an element:


multimap authors;
// adds the first element with the key Barth, John
authors.insert({"Barth, John", "Sot-Weed Factor"});
// ok: adds the second element with the key Barth, John
authors.insert({"Barth, John", "Lost in the Funhouse"});

For the containers that allow multiple keys, the insertoperation that takes a single element returns an iterator to the new element. There is no need to return a bool, because insert always adds a new element in these types.


11.3.3 ERASING ELEMENTS
Table 11.5. Removing E