最終更新日時(UTC):
が更新

履歴 編集

function
<vector>

std::vector::cbegin(C++11)

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;
  });
}

出力

1
2
3

バージョン

言語

  • C++11

処理系

参照