public member function
<unordered_map>

std::unordered_map::begin

container iterator (1)
      iterator begin() noexcept;
const_iterator begin() const noexcept;
bucket iterator (2)
      local_iterator begin ( size_type n );
const_local_iterator begin ( size_type n ) const;
Return iterator to beginning
Returns an iterator pointing to the first element in the unordered_map container (1) or in one of its buckets (2).

Notice that an unordered_map object makes no guarantees on which specific element is considered its first element. But, in any case, the range that goes from its begin to its end covers all the elements in the container (or the bucket), until invalidated.

Parameters

n
Bucket number. This shall be lower than bucket_count.
It is an optional parameter that changes the behavior of this member function: if set, the iterator retrieved points to the first element of a bucket, otherwise it points to the first element of the container.
Member type size_type is an unsigned integral type.

Return Value

An iterator to the first element in the container (1) or the bucket (2).

All return types (iterator, const_iterator, local_iterator and const_local_iterator) are member types. In the unordered_map class template, these are forward iterator types.
Local iterators are of the same category as non-local iterators. Their value_type, difference_type, pointer and reference member types are also the same. But the iterators themselves are not necessarily of the same type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// unordered_map::begin/end example
#include <iostream>
#include <unordered_map>

int main ()
{
  std::unordered_map<std::string,std::string> mymap;
  mymap = {{"Australia","Canberra"},{"U.S.","Washington"},{"France","Paris"}};

  std::cout << "mymap contains:";
  for ( auto it = mymap.begin(); it != mymap.end(); ++it )
    std::cout << " " << it->first << ":" << it->second;
  std::cout << std::endl;

  std::cout << "mymap's buckets contain:\n";
  for ( unsigned i = 0; i < mymap.bucket_count(); ++i) {
    std::cout << "bucket #" << i << " contains:";
    for ( auto local_it = mymap.begin(i); local_it!= mymap.end(i); ++local_it )
      std::cout << " " << local_it->first << ":" << local_it->second;
    std::cout << std::endl;
  }

  return 0;
}


Possible output:
mymap contains: France:Paris Australia:Canberra U.S.:Washington
mymap's buckets contain:
bucket #0 contains:
bucket #1 contains:
bucket #2 contains:
bucket #3 contains:
bucket #4 contains:
bucket #5 contains: France:Paris
bucket #6 contains:
bucket #7 contains: Australia:Canberra
bucket #8 contains: U.S.:Washington
bucket #9 contains:
bucket #10 contains:

Complexity

Constant.

Iterator validity

No changes.

See also

  • 9316991614 2018-02-25
  • 191841613 2018-02-25
  • 16411612 2018-02-25
  • 6081981611 2018-02-25
  • 1784561610 2018-02-25
  • 4651609 2018-02-25
  • 9348111608 2018-02-25
  • 2513771607 2018-02-25
  • 6478461606 2018-02-24
  • 4985791605 2018-02-24
  • 5637141604 2018-02-24
  • 282181603 2018-02-24
  • 6217941602 2018-02-24
  • 5076141601 2018-02-24
  • 714281600 2018-02-24
  • 6607141599 2018-02-24
  • 949041598 2018-02-24
  • 6809961597 2018-02-24
  • 671871596 2018-02-24
  • 7107821595 2018-02-24