Skip to content

Latest commit

 

History

History
71 lines (52 loc) · 1.7 KB

File metadata and controls

71 lines (52 loc) · 1.7 KB

cbegin

  • vector[meta header]
  • std[meta namespace]
  • vector[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
const_iterator cbegin() const noexcept;           // (1) C++11
constexpr const_iterator cbegin() const noexcept; // (1) C++20

概要

先頭の要素を指す読み取り専用イテレータを取得する。

begin()は非constvectorオブジェクトに対してiteratorを返し、constvectorオブジェクトに対してはconst_iteratorを返すが、cbegin()const_iteratorを返すバージョンのみが提供されている。

アルゴリズムにイテレータの組を渡す際、アルゴリズム内でデータの書き換えが起こらないというユーザーの意図を示す場合などに有用である。

戻り値

先頭の要素を指す読み取り専用イテレータ

例外

投げない

計算量

定数時間

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<int> v = {1, 2, 3};

  // このアルゴリズム内ではvの書き換えを決して行わない
  std::for_each(v.cbegin(), v.cend(), [](const int& x) {
    std::cout << x << std::endl;
  });
}
  • cbegin()[color ff0000]
  • v.cend()[link cend.md]

出力

1
2
3

バージョン

言語

  • C++11

処理系

  • Clang: ??
  • GCC: 4.7.0 [mark verified]
  • ICC: ??
  • Visual C++: 2010 [mark verified], 2012 [mark verified], 2013 [mark verified]

参照