Namespaces
Variants

std::array<T,N>:: begin, std::array<T,N>:: cbegin

From cppreference.net

iterator begin ( ) noexcept ;
(1) (C++11以降)
(constexprはC++17以降)
const_iterator begin ( ) const noexcept ;
(2) (C++11以降)
(constexprはC++17以降)
const_iterator cbegin ( ) const noexcept ;
(3) (C++11以降)
(constexprはC++17以降)

* this の最初の要素へのイテレータを返します。

* this が空の場合、返されるイテレータは end() と等しくなります。

range-begin-end.svg

目次

翻訳の説明: - 「Contents」を「目次」に翻訳 - C++関連の専門用語(Return value、Complexity、Example、See also)は原文のまま保持 - HTMLタグ、属性、クラス名は一切変更せず - 数値、リンク、構造は完全に保持 - フォーマットとインデントは元のまま維持

戻り値

最初の要素へのイテレータ。

計算量

定数。

#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
int main()
{
    std::cout << std::boolalpha;
    std::array<int, 0> empty;
    std::cout << "1) "
              << (empty.begin() == empty.end()) << ' '     // true
              << (empty.cbegin() == empty.cend()) << '\n'; // true
    // *(empty.begin()) = 42; // => 実行時未定義動作
    std::array<int, 4> numbers{5, 2, 3, 4};
    std::cout << "2) "
              << (numbers.begin() == numbers.end()) << ' '    // false
              << (numbers.cbegin() == numbers.cend()) << '\n' // false
              << "3) "
              << *(numbers.begin()) << ' '    // 5
              << *(numbers.cbegin()) << '\n'; // 5
    *numbers.begin() = 1;
    std::cout << "4) " << *(numbers.begin()) << '\n'; // 1
    // *(numbers.cbegin()) = 42; // コンパイル時エラー: 
                                 // 読み取り専用変数は代入不可
    // 全要素を出力
    std::cout << "5) ";
    std::for_each(numbers.cbegin(), numbers.cend(), [](int x)
    {
        std::cout << x << ' ';
    });
    std::cout << '\n';
    constexpr std::array constants{'A', 'B', 'C'};
    static_assert(constants.begin() != constants.end());   // OK
    static_assert(constants.cbegin() != constants.cend()); // OK
    static_assert(*constants.begin() == 'A');              // OK
    static_assert(*constants.cbegin() == 'A');             // OK
    // *constants.begin() = 'Z'; // コンパイル時エラー: 
                                 // 読み取り専用変数は代入不可
}

出力:

1) true true
2) false false
3) 5 5
4) 1
5) 1 2 3 4

関連項目

終端へのイテレータを返す
(公開メンバ関数)
(C++11) (C++14)
コンテナまたは配列の先頭へのイテレータを返す
(関数テンプレート)