Professional C__ - Marc Gregoire [217]
Scott K.(Phone: 654-987321)
-------
John D.(Phone: 321-987654)
Zulija N.(Phone: 987-654321)
Marc G.(Phone: 123-456789)
Johan G.(Phone: 963-258147)
Scott K.(Phone: 654-987321)
-------
Marc G. is in bucket 4 which contains the following 1 elements:
Marc G.(Phone: 123-456789)
-------
There are 11 buckets.
Average number of elements in a bucket is 0.454545
unordered_multimap
The unordered_multimap is an unordered_map that allows multiple elements with the same key. Their interfaces are almost identical, with the following changes:
unordered_multimaps do not provide operator[]. The semantics of this operator does not make sense if there can be multiple elements with a single key.
Inserts on unordered_multimaps always succeed. Thus, the unordered_multimap insert() method that adds a single element returns only an iterator.
unordered_multimaps allow you to insert identical key/value pairs. If you want to avoid this redundancy, you must check explicitly before inserting a new element.
As discussed earlier with multimaps, looking up elements in unordered_multimaps cannot be done using operator[] because it is not provided. You can use find() but it will return an iterator referring to any one of the elements with a given key (not necessarily the first element with that key). Instead, it’s best to use the equal_range() method, which will return a pair of iterators: one referring to the first element matching a given key and one referring to one-past-the-last element matching a given key. The use of equal_range() is exactly the same as discussed in the context of the multimap, so you can refer to the example given for multimaps to see how it works.
unordered_set/unordered_multiset
The OTHER CONTAINERS Standard C-Style Arrays Recall that “dumb” pointers are bona fide iterators because they support the required operators. This point is more than just a piece of trivia. It means that you can treat standard C-style arrays as STL containers by using pointers to their elements as iterators. Standard C-style arrays, of course, don’t provide methods like size(), empty(), insert(), and erase(), so they aren’t true STL containers. Nevertheless, because they do support iterators through pointers, you can use them in the algorithms described in Chapter 13 and in some of the methods described in this chapter. For example, you could copy all the elements of a standard C-style array into a vector using the insert() method of a vector that takes an iterator range from any container. This insert() method prototype looks like this: template InputIterator first, InputIterator last); If you want to use a standard C-style int array as the source, then the templatized type of InputIterator becomes int*. Here is the full example: int arr[10]; // standard C-style array vector // Initialize each element of the array to the value of its index. for (int i = 0; i < 10; i++) { arr[i] = i; } // Insert the contents of the array into the end of the vector. vec.insert(vec.end(), arr, arr + 10); // Print the contents of the vector. for (auto& i : vec) { cout << i << " "; } Code snippet from ArrayIterators\ArrayIterators.cpp Note that the iterator referring to the first element of the array is the address of
There are several other parts of the C++ language that work with the STL to varying degrees, including standard C-style arrays, strings, streams, and the bitset.