public member function
<unordered_map>

std::unordered_map::cbegin

container iterator (1)
const_iterator cbegin() const noexcept;
bucket iterator (2)
const_local_iterator cbegin ( size_type n ) const;
返回指向开头的 const_iterator
返回一个const_iteratorpointing to the first element in the unordered_map container (1) or in one of its buckets (2).

Aconst_iterator是一个指向常量内容的迭代器。这个迭代器可以像iteratorreturned by unordered_map::begin, but it cannot be used to modify the contents it points to.

参数

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.
成员类型size_type是一种无符号整型类型。

返回值

Aconst_iteratorto the first element in the container (1) or the bucket (2).

两者都const_iteratorconst_local_iteratorare member types. In the unordered_map class template, these are forward iterator types.
const_local_iteratoris an interator of the same category asconst_iterator. Theirvalue_type, difference_type, 指针引用member types are also the same. But the iterators themselves are not necessarily of the same type.

示例

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::cbegin/cend 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.cbegin(); it != mymap.cend(); ++it )
    std::cout << " " << it->first << ":" << it->second;  // cannot modify *it
  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.cbegin(i); local_it!= mymap.cend(i); ++local_it )
      std::cout << " " << local_it->first << ":" << local_it->second;
    std::cout << std::endl;
  }

  return 0;
}

可能的输出
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:


复杂度

常量。

迭代器有效性

没有变化。

另见