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

履歴 編集

function template
<iterator>

std::cbegin(C++14)

namespace std {
  template <class C>
  constexpr auto cbegin(const C& c)
    noexcept(noexcept(std::begin(c)))
    -> decltype(std::begin(c));
}

概要

範囲から先頭要素への読み取り専用イテレータを取得する。

戻り値

return std::begin(c);

パラメータをconstで受け取っているので、std::begin()を経由することで、読み取り専用イテレータを取得している。

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

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

  decltype(v)::const_iterator first = std::cbegin(v);
  decltype(v)::const_iterator last = std::cend(v);

  std::for_each(first, last, [](const int& x) {
    std::cout << x << std::endl;
  });
}

出力

1
2
3

バージョン

言語

  • C++14

処理系

参照