Chapter 11 Associative Containers
Using an Associative Container
- An overview of associative containers:
- Range
foryieldspairs of keys and corresponding values.
- Range

Overview of the Associative Containers
- Associative containers support general container operations except for position-specific operations (
push_front,back, etc.). - Initializing a map or a set:
map<string, size_t> word_count;
map<string, string> authors = { {"Joyce", "James"}, {"Austen", "Jane"} };
set<string> exclude = {"the", "but", "and", "or", "an", "a", "A"};
set<string> iset(ivec.cbegin(), iven.cend());
multiset<string> miset(ivec.cbegin(), iven.cend());
- Key type requirements: comparable by
<(ordered containers) or hashable and comparable by==(unordered containers).- Strict weak ordering: if neither key is less than the other, then they are equivalent.
- To designate the comparison function, use a function pointer as the type in the type definition, and pass the function (or a pointer) as the constructor argument:
muliset<T, decltype(cmp)*> s(cmp).
pair:- A
paircan be initialized in these ways:p(value initialized),p(v1, v2),p{v1, v2},p = {v1, v2},make_pair(v1, v2)(types are inferred). - The data members of
pair(defined inutility) arepublic:firstandsecondcan be accessed. pairs can be compared (relational operators).
- A
Operations on Associative Containers
key_type,mapped_type,value_typeare defined in each container.value_typeiskey_type(set) orpair(map).- Dereferencing iterators will yield a
value_typereference. The key isconst. So forsettypes, the iterators areconst. - When an iterator is used to traverse an ordered container, the keys are in ascending order.
insertercan be used to make the associative container the destination of an algorithm.- Insertion operations (for ordered associative containers, the location to insert will be searched to maintain the ascending order):
- For containers with unique keys, a
pairwill be returned, containing an iterator to the element and aboolindicating whether the element is inserted or duplicate. - For containers that allow multiple keys, an iterator will be returned.
- For containers with unique keys, a

erasetakes arguments of a key, an iterator, or an iterator range. It returns the number of elements removed.mapandunordered_mapprovide the subscript operator and theatfunction that return an lvalue. Ifm[key]does not exist, a new element is created and value initialized, so the subscript operator should not be called on aconstmap. However,m.at(key)will not create a new one and will throw anout_of_rangeexception, so it can used onconstmaps.- Ways of accessing/counting elements:
- Elements of the same key will be adjacent to each other in a
multimap/multiset. Thus, we can usefindandcount, orlower_boundandupper_bound, orequal_range(a pair of iterators) to locate the range of elements.
- Elements of the same key will be adjacent to each other in a

The Unordered Containers
- Unordered container management operations (not used normally):

- A
hash<key_type>object will be used to generate the hash code. The hash function for ordinary types of keys have been implemented. It needs to be supplied if we want to use it on customized class types. A simple strategy is to wrap an implemented hash function.- The constructor accepts arguments of
(bucket_size, hasher, eqOp). - If the
==operator is defined in class, only the hash function needs to be overridden.
- The constructor accepts arguments of
size_t hasher(const Sales_data &sd) {
return hash<string>()(sd.isbn());
}
bool eqOp(const Sales_data &lhs, const Sales_data &rhs) {
return lhs.isbn() == rhs.isbn();
}
using SD_multiset = unordered_multiset<Sales_data, decltype(hasher)*, decltype(eqOp)*>;
SD_multiset bookstore(42, hasher, eqOp);