public member type
<unordered_set>

std::unordered_set::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_set container (1) or in one of its buckets (2).

Notice that an unordered_set 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.

All iterators in an unordered_set have const access to the elements (even those whose type is not prefixed with const_): Elements can be inserted or removed, but not modified while in the container.

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_set class template, these are forward iterator types.

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
25
// unordered_set::begin/end example
#include <iostream>
#include <string>
#include <unordered_set>

int main ()
{
  std::unordered_set<std::string> myset =
  {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"};

  std::cout << "myset contains:";
  for ( auto it = myset.begin(); it != myset.end(); ++it )
    std::cout << " " << *it;
  std::cout << std::endl;

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

  return 0;
}


Possible output:
myset contains: Venus Jupiter Neptune Mercury Earth Uranus Saturn Mars
myset's buckets contain:
bucket #0 contains:
bucket #1 contains: Venus
bucket #2 contains: Jupiter
bucket #3 contains: 
bucket #4 contains: Neptune Mercury
bucket #5 contains: 
bucket #6 contains: Earth
bucket #7 contains: Uranus Saturn
bucket #8 contains: Mars
bucket #9 contains: 
bucket #10 contains: 

Complexity

Constant.

Iterator validity

No changes.

See also

  • 997351548 2018-02-20
  • 903691547 2018-02-20
  • 9127171546 2018-02-20
  • 2234751545 2018-02-20
  • 1685381544 2018-02-20
  • 911091543 2018-02-20
  • 9281021542 2018-02-20
  • 632231541 2018-02-20
  • 9751491540 2018-02-20
  • 8333621539 2018-02-20
  • 995001538 2018-02-20
  • 6477441537 2018-02-20
  • 511981536 2018-02-19
  • 2072881535 2018-02-19
  • 6024461534 2018-02-19
  • 7594061533 2018-02-19
  • 9311341532 2018-02-19
  • 4928331531 2018-02-19
  • 38561530 2018-02-19
  • 7776611529 2018-02-19