C++ Array 库 - cbegin() 函数
描述
C++ 函数 std::array::cbegin() 返回一个指向数组开头的常量迭代器。 此方法返回的迭代器可用于迭代容器,但不能用于修改数组内容。
声明
以下是 std::array::cbegin() 函数形式 std::array 头的声明。
const_iterator cbegin() const noexcept;
参数
None
返回值
返回一个指向数组开头的常量迭代器。
异常
此成员函数从不抛出异常。
时间复杂度
常数,即 O(1)
示例
以下示例显示了 std::array::cbegin() 函数的用法。
#include <iostream>
#include <array>
using namespace std;
int main(void) {
array<int, 5> arr = {1, 2, 3, 4, 5};
auto it = arr.cbegin(); /* returns a constant iterator */
/* ERROR: attemp to modify value will report compilation error */
*it = 100;
return 0;
}
上述程序的编译将失败并显示以下错误消息。
cbegin.cpp: In function ‘int main()’:
cbegin.cpp:12:8: error: assignment of read-only location ‘* it’
*it = 100;
^

