Namespaces
Variants

std::array<T,N>:: end, std::array<T,N>:: cend

From cppreference.net

iterator end ( ) noexcept ;
(1) (自 C++11 起)
(自 C++17 起为 constexpr)
const_iterator end ( ) const noexcept ;
(2) (自 C++11 起)
(自 C++17 起为 constexpr)
const_iterator cend ( ) const noexcept ;
(3) (自 C++11 起)
(自 C++17 起为 constexpr)

返回指向 * this 最后一个元素之后位置的迭代器。

此返回的迭代器仅作为哨兵使用。不保证其具有 可解引用性

range-begin-end.svg

目录

返回值

指向最后一个元素之后的迭代器。

复杂度

常量。

示例

#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)
返回指向容器或数组末尾的迭代器
(函数模板)