Namespaces
Variants

std:: array

From cppreference.net
定义于头文件 <array>
template <

class T,
std:: size_t N

> struct array ;
(C++11 起)

std::array 是一种封装固定大小数组的容器。

此容器是一个聚合类型,其语义与持有 T [ N ] 作为唯一非静态数据成员的结构体相同。与C风格数组不同,它不会自动退化为 T * 。作为聚合类型,它可以通过 聚合初始化 进行初始化,最多接受 N 个可转换为 T 的初始化器: std :: array < int , 3 > a = { 1 , 2 , 3 } ;

该结构体结合了C风格数组的性能和可访问性,以及标准容器的优势,例如知晓自身大小、支持赋值、随机访问迭代器等。

std::array 满足 Container ReversibleContainer 的要求,但默认构造的数组非空且交换操作具有线性复杂度, 满足 ContiguousContainer 的要求, (C++17 起) 并部分满足 SequenceContainer 的要求。

对于零长度数组( N == 0 )存在特殊情况。此时, array. begin ( ) == array. end ( ) 成立,该值为某个唯一值。对零尺寸数组调用 front ( ) back ( ) 将导致未定义行为。

数组也可以用作包含 N 个相同类型元素的元组。

目录

迭代器失效

通常来说,指向数组的迭代器在数组的整个生命周期内都不会失效。但需要注意,在 swap 操作期间,迭代器会继续指向同一个数组元素,因此其指向的值会发生改变。

模板参数

T - 元素类型 必须满足 MoveConstructible MoveAssignable 要求。
N - 数组中的元素数量或 0

成员类型

成员类型 定义
value_type T
size_type std::size_t
difference_type std::ptrdiff_t
reference value_type &
const_reference const value_type &
pointer value_type *
const_pointer const value_type *
iterator

LegacyRandomAccessIterator LegacyContiguousIterator 指向 value_type

(C++17 前)

LegacyRandomAccessIterator LegacyContiguousIterator 且为 LiteralType 指向 value_type

(C++17 起)
(C++20 前)

LegacyRandomAccessIterator contiguous_iterator ConstexprIterator 指向 value_type

(C++20 起)
const_iterator

LegacyRandomAccessIterator LegacyContiguousIterator 指向 const value_type

(C++17 前)

LegacyRandomAccessIterator LegacyContiguousIterator 且为 LiteralType 指向 const value_type

(C++17 起)
(C++20 前)

LegacyRandomAccessIterator contiguous_iterator ConstexprIterator 指向 const value_type

(C++20 起)
reverse_iterator std:: reverse_iterator < iterator >
const_reverse_iterator std:: reverse_iterator < const_iterator >

成员函数

隐式定义的成员函数
(constructor)
(implicitly declared)
遵循 聚合初始化 规则初始化数组(注意对于非类类型 T ,默认初始化可能导致不确定值)
(public member function)
(destructor)
(implicitly declared)
销毁数组的每个元素
(public member function)
operator=
(implicitly declared)
用另一个数组的对应元素覆盖当前数组的每个元素
(public member function)
元素访问
带边界检查的指定元素访问
(public member function)
访问指定元素
(public member function)
访问首元素
(public member function)
访问末元素
(public member function)
直接访问底层连续存储
(public member function)
迭代器
返回指向起始位置的迭代器
(public member function)
返回指向末尾位置的迭代器
(public member function)
返回指向起始位置的逆向迭代器
(public member function)
返回指向末尾位置的逆向迭代器
(public member function)
容量
检查容器是否为空
(public member function)
返回元素数量
(public member function)
返回可能的最大元素数量
(public member function)
操作
用指定值填充容器
(public member function)
交换内容
(public member function)

非成员函数

(C++11) (C++11) (removed in C++20) (C++11) (removed in C++20) (C++11) (removed in C++20) (C++11) (removed in C++20) (C++11) (removed in C++20) (C++20)
按字典序比较两个 array 的值
(函数模板)
访问 array 的元素
(函数模板)
特化 std::swap 算法
(函数模板)
(C++20)
从内置数组创建 std::array 对象
(函数模板)

辅助类

获取 array 的大小
(类模板特化)
获取 array 元素的类型
(类模板特化)

推导指引

(C++17 起)

示例

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
    // 构造使用聚合初始化
    std::array<int, 3> a1{{1, 2, 3}}; // C++11 中在 CWG 1270 修订前需要双花括号
                                      //(修订后的 C++11 及 C++14 之后无需)
    std::array<int, 3> a2 = {1, 2, 3}; // 在 = 后永远不需要双花括号
    // 支持容器操作
    std::sort(a1.begin(), a1.end());
    std::ranges::reverse_copy(a2, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    // 支持范围 for 循环
    std::array<std::string, 2> a3{"E", "\u018E"};
    for (const auto& s : a3)
        std::cout << s << ' ';
    std::cout << '\n';
    // 数组创建的推导指南(C++17 起)
    [[maybe_unused]] std::array a4{3.0, 1.0, 4.0}; // std::array<double, 3>
    // 未指定元素的行为与内置数组相同
    [[maybe_unused]] std::array<int, 2> a5; // 无列表初始化,a5[0] 和 a5[1]
                                            // 为默认初始化
    [[maybe_unused]] std::array<int, 2> a6{}; // 列表初始化,两个元素均为值
                                              // 初始化,a6[0] = a6[1] = 0
    [[maybe_unused]] std::array<int, 2> a7{1}; // 列表初始化,未指定元素为值
                                               // 初始化,a7[0] = 1, a7[1] = 0
}

输出:

3 2 1
E Ǝ

另请参阅

可调整大小、固定容量、就地连续数组
(类模板)
可调整大小的连续数组
(类模板)
双端队列
(类模板)
(library fundamentals TS v2)
创建大小和可选元素类型可从参数推导的 std::array 对象
(函数模板)