11.3.4 SUBSCRIPTING A MAP
The map and unordered_map containers provide the subscript operator and a corresponding at function. The set types do not support subscripting because there is no “value” associated with a key in a set. The elements are themselves keys, so the operation of “fetching the value associated with a key” is meaningless. We cannot subscript a multimap or an unordered_multimap because there may be more than one value associated with a given key.

map
word_count["Anna"] = 1;
Because the subscript operator might insert an element, we may use subscript only on a map that is not const.
Ordinarily, the type returned by dereferencing an iterator and the type returned by the subscript operator are the same. Not so for maps: when we subscript a map, we get a mapped_type object; when we dereference a map iterator, we get a value_type object
In common with other subscripts, the map subscript operator returns an lvalue. Because the return is an lvalue, we can read or write the element:
The fact that the subscript operator adds an element if it is not already in the map allows us to write surprisingly succinct programs such as the loop inside our wordcounting program. On the other hand, sometimes we only want to know whether an element is present and do notwant to add the element if it is not. In such cases, we must not use the subscript operator.
The associative containers provide various ways to find a given element, which are described in Table 11.7.
Sometimes,we want to know if an element with a given key is present without changing the map. We cannot use the subscript operator to determine whether an element is present, because the subscript operator inserts a new element if the key is not already there. In such cases, we should use find:
cout << "foobar is not in the map" << endl;
Finding Elements in a multimap or multiset:
auto entries = authors.count(search_item); // number of elements
auto iter = authors.find(search_item); // first entry for this author
// loop through the number of entries there are for this author
while(entries) {
cout << iter->second << endl; // print each title
++iter; // advance to the next title
--entries; // keep track of how many we've printed
}
// definitions of authors and search_item as above
// beg and end denote the range of elements for this author
for (auto beg = authors.lower_bound(search_item),end = authors.upper_bound(search_item);beg != end; ++beg)
cout << beg->second << endl; // print each title
This function takes a key and returns a pairof iterators. If the key is present, then the first iterator refers to the first instance of the key and the second iterator refers one past the last instance of the key.
// definitions of authors and search_item as above
// pos holds iterators that denote the range of elements for this key
for (auto pos = authors.equal_range(search_item);
pos.first != pos.second; ++pos.first)
cout << pos.first->second << endl; // print each title
The new standard defines four unordered associative containers. Rather than using a comparison operation to organize their elements, these containers use a hash function and the key type’s ==operator. An unordered container is most useful when we have a key type for which there is no obvious ordering relationship among the