命名空间
变体
操作

std::array

来自 cppreference.cn
< cpp‎ | container
 
 
 
 
定义于头文件 <array>
template<

    class T,
    std::size_t N

> struct array;
(C++11 起)

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

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

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

std::array 满足容器 (Container) 和可逆容器 (ReversibleContainer) 的要求,除了默认构造的 array 不为空以及交换的复杂度是线性的,满足连续容器 (ContiguousContainer) 的要求,(C++17 起) 并且部分满足序列容器 (SequenceContainer) 的要求。

对于零长度数组(N == 0)有一个特殊情况。在这种情况下,array.begin() == array.end(),它是一个唯一的某个值。在零大小的 array 上调用 front()back() 的效果是未定义的。

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

目录

[编辑] 迭代器失效

通常情况下,指向 array 的迭代器在 array 的整个生命周期内永远不会失效。但需要注意,在交换操作期间,迭代器将继续指向相同的数组元素,因此其值会发生改变。

[编辑] 模板参数

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

指向 value_type遗留随机访问迭代器 (LegacyRandomAccessIterator) 和遗留连续迭代器 (LegacyContiguousIterator)

(C++17 前)

指向 value_type 的,身为字面类型 (LiteralType) 的遗留随机访问迭代器 (LegacyRandomAccessIterator) 和遗留连续迭代器 (LegacyContiguousIterator)

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

指向 value_type遗留随机访问迭代器 (LegacyRandomAccessIterator)、连续迭代器 (contiguous_iterator) 和常量表达式迭代器 (ConstexprIterator)

(C++20 起)
[编辑]
const_iterator

指向 const value_type遗留随机访问迭代器 (LegacyRandomAccessIterator) 和遗留连续迭代器 (LegacyContiguousIterator)

(C++17 前)

指向 const value_type 的,身为字面类型 (LiteralType) 的遗留随机访问迭代器 (LegacyRandomAccessIterator) 和遗留连续迭代器 (LegacyContiguousIterator)

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

指向 const value_type遗留随机访问迭代器 (LegacyRandomAccessIterator)、连续迭代器 (contiguous_iterator) 和常量表达式迭代器 (ConstexprIterator)

(C++20 起)
[编辑]
reverse_iterator std::reverse_iterator<iterator>[编辑]
const_reverse_iterator std::reverse_iterator<const_iterator>[编辑]

[编辑] 成员函数

隐式定义的成员函数
(构造函数)
(隐式声明)
遵循聚合初始化规则初始化数组(注意,对于非类类型的 T,默认初始化可能导致不确定的值)
(公开成员函数)
(析构函数)
(隐式声明)
销毁数组的每个元素
(公开成员函数)
operator=
(隐式声明)
用另一个数组的对应元素覆写数组的每个元素
(公开成员函数)
元素访问
访问指定的元素,带边界检查
(公开成员函数) [编辑]
访问指定的元素
(公开成员函数) [编辑]
访问第一个元素
(公开成员函数) [编辑]
访问最后一个元素
(公开成员函数) [编辑]
直接访问底层连续存储
(公开成员函数) [编辑]
迭代器
返回指向起始的迭代器
(公开成员函数) [编辑]
返回指向末尾的迭代器
(公开成员函数) [编辑]
返回指向起始的逆向迭代器
(公开成员函数) [编辑]
返回指向末尾的逆向迭代器
(公开成员函数) [编辑]
容量
检查容器是否为空
(公开成员函数) [编辑]
返回元素数量
(公开成员函数) [编辑]
返回元素的最大可能数量
(公开成员函数) [编辑]
操作
用指定值填充容器
(公开成员函数) [编辑]
交换内容
(公开成员函数) [编辑]

[编辑] 非成员函数

(C++11 起)(C++11 起)(C++20 中移除)(C++11 起)(C++20 中移除)(C++11 起)(C++20 中移除)(C++11 起)(C++20 中移除)(C++11 起)(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()
{
    // Construction uses aggregate initialization
    std::array<int, 3> a1{{1, 2, 3}}; // Double-braces required in C++11 prior to
                                      // the CWG 1270 revision (not needed in C++11
                                      // after the revision and in C++14 and beyond)
 
    std::array<int, 3> a2 = {1, 2, 3}; // Double braces never required after =
 
    // Container operations are supported
    std::sort(a1.begin(), a1.end());
    std::ranges::reverse_copy(a2, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
 
    // Ranged for loop is supported
    std::array<std::string, 2> a3{"E", "\u018E"};
    for (const auto& s : a3)
        std::cout << s << ' ';
    std::cout << '\n';
 
    // Deduction guide for array creation (since C++17)
    [[maybe_unused]] std::array a4{3.0, 1.0, 4.0}; // std::array<double, 3>
 
    // Behavior of unspecified elements is the same as with built-in arrays
    [[maybe_unused]] std::array<int, 2> a5; // No list init, a5[0] and a5[1]
                                            // are default initialized
    [[maybe_unused]] std::array<int, 2> a6{}; // List init, both elements are value
                                              // initialized, a6[0] = a6[1] = 0
    [[maybe_unused]] std::array<int, 2> a7{1}; // List init, unspecified element is value
                                               // initialized, a7[0] = 1, a7[1] = 0
}

输出

3 2 1
E Ǝ

[编辑] 参阅

可变大小、固定容量、原地连续数组
(类模板) [编辑]
可变大小的连续数组
(类模板) [编辑]
双端队列
(类模板) [编辑]
(库基础 TS v2)
创建一个 std::array 对象,其大小和可选的元素类型从参数中推导
(函数模板) [编辑]