跳到主要内容

std::array operator[]

// Non const version
constexpr reference at( size_type pos );

// Const version
constexpr const_reference at( size_type pos ) const;

返回对指定索引pos处元素的引用

指向指定索引 pos 处的字符。不执行边界检查。

参数

  • pos - 要返回的字符的位置

返回值

对所请求字符的引用。

异常

无。

复杂度

常数。

示例

Main.cpp
#include <array>
#include <iostream>

int main()
{
std::array<int,4> numbers {2, 4, 6, 8};

std::cout << "Second element: " << numbers[1] << '\n';

numbers[0] = 5;

std::cout << "All numbers:";
for (auto i : numbers) {
std::cout << ' ' << i;
}
std::cout << '\n';
}
输出
Second element: 4
All numbers: 5 4 6 8
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而有所修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::array operator[]

// Non const version
constexpr reference at( size_type pos );

// Const version
constexpr const_reference at( size_type pos ) const;

返回对指定索引pos处元素的引用

指向指定索引 pos 处的字符。不执行边界检查。

参数

  • pos - 要返回的字符的位置

返回值

对所请求字符的引用。

异常

无。

复杂度

常数。

示例

Main.cpp
#include <array>
#include <iostream>

int main()
{
std::array<int,4> numbers {2, 4, 6, 8};

std::cout << "Second element: " << numbers[1] << '\n';

numbers[0] = 5;

std::cout << "All numbers:";
for (auto i : numbers) {
std::cout << ' ' << i;
}
std::cout << '\n';
}
输出
Second element: 4
All numbers: 5 4 6 8
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而有所修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。