4

When I'm using a std::unordered_map<K, V> I know that the iterator to each key-value pair is of type std::unordered_map<K, V>::iterator. I also know that the iterator itself points to a pair<const K, V>. However, the only reason I know the iterator points to a pair is from looking at example code. Where is this behavior defined?

For example, if I go to the documentation at cppreference.com, I don't see where this behavior is explained. It only says that the member iterator is defined as a ForwardIterator.

So, my question is, how would a smart developer know what a std::unordered_map<K, V>::iterator actually represents? I'm sure there is some logical leap I'm missing.

Peter Moran
  • 295
  • 3
  • 13
  • 1
    That mention of [`ForwardIterator`](http://en.cppreference.com/w/cpp/concept/ForwardIterator) is a clickable link. Of further interest: "`std::unordered_map` meets the requirements of `Container`, `AllocatorAwareContainer`, `UnorderedAssociativeContainer`." Each of these camel-case identifiers are also links, with further information about how a container is expected to behave. – Igor Tandetnik Oct 26 '17 at 05:19
  • cppreference.com is a public wiki. Please go fix that page and make it clearer. I'll do the same. – Gabriel Staples Feb 11 '21 at 22:08
  • Done. I edited the page and added an answer here: https://stackoverflow.com/a/66164132/4561887. Hopefully the edit sticks. If not, use the cplusplus.com reference page instead (http://www.cplusplus.com/reference/unordered_map/unordered_map/) as it makes it _crystal clear_ when it says an `iterator` is: `a forward iterator to value_type` and a `const_iterator` is `a forward iterator to const value_type`, and is shows the `value_type` to be `pair`. – Gabriel Staples Feb 11 '21 at 22:54
  • See also: https://stackoverflow.com/questions/6963894/how-to-use-range-based-for-loop-with-stdmap/6963910#6963910. – Gabriel Staples Feb 11 '21 at 23:04

2 Answers2

1

For STL containers

objects of type iterator when de-referenced return an object of type reference, which is a reference to an object of type value_type.

These are all defined inside the container.

Note the std::map is defined as a container. This information is part of this documentation.

https://en.cppreference.com/w/cpp/named_req/Container

Martin York
  • 257,169
  • 86
  • 333
  • 562
0

So, my question is, how would a smart developer know what a std::unordered_map<K, V>::iterator actually represents? I'm sure there is some logical leap I'm missing.

As of a few minutes ago, this page (https://en.cppreference.com/w/cpp/container/unordered_map) now shows the image below.

Notice the sections I highlighted. It now makes it crystal clear that an iterator is "to value_type", and a const iterator is "to const value_type". And value_type is specified as std::pair<const Key, T>. So, dereferencing an interator (*my_iterator) provides you an object of type value_type, which is std::pair<const Key, T> in this case--for a std::unordered_map. This should make it all clear now.

enter image description here

References:

Related links I've found really helpful as I've studied this, in this order from most-to-least helpful:

  1. [MOST EXCELLENT article] https://thispointer.com/how-to-iterate-over-an-unordered_map-in-c11/
  2. [EXCELLENT GENERAL INFO on iterators] https://www.cplusplus.com/reference/iterator/
  3. [cplusplus.com std::unordered_map reference pg] https://www.cplusplus.com/reference/unordered_map/unordered_map/
  4. [cppreference.com std::unordered_map reference pg] https://en.cppreference.com/w/cpp/container/unordered_map

See also:

  1. How to use range-based for() loop with std::map?
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265