命名空间
变体
操作

标准库头文件 <array> (C++11)

来自 cppreference.com
< cpp‎ | 头文件
 
 
标准库头文件
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用实用程序
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<array> (C++11)
<deque>
<forward_list> (C++11)
<list>
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

此头文件是 容器 库的一部分。

内容

包含

(C++20)
三路比较运算符 支持[编辑]
std::initializer_list 类模板[编辑]

(C++11)
固定大小的原地连续数组
(类模板) [编辑]
获取元组类型元素的个数
(类模板) [编辑]
获取元组类型元素的类型
(类模板) [编辑]
获取array的大小
(类模板特化) [编辑]
获取array元素的类型
(类模板特化) [编辑]

函数

(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的值
(函数模板) [编辑]
特化std::swap 算法
(函数模板) [编辑]
(C++20)
从内置数组创建std::array对象
(函数模板) [编辑]
访问array的元素
(函数模板) [编辑]
范围访问
(C++11)(C++14)
返回指向容器或数组开头的迭代器
(函数模板) [编辑]
(C++11)(C++14)
返回指向容器或数组末尾的迭代器
(函数模板) [编辑]
返回指向容器或数组开头的反向迭代器
(函数模板) [编辑]
(C++14)
返回容器或数组的反向末尾迭代器
(函数模板) [编辑]
(C++17)(C++20)
返回容器或数组的大小
(函数模板) [编辑]
(C++17)
检查容器是否为空
(函数模板) [编辑]
(C++17)
获取指向底层数组的指针
(函数模板) [编辑]

[编辑] 概要

// mostly freestanding
#include <compare>
#include <initializer_list>
 
namespace std {
  // class template array
  template<class T, size_t N>
  struct array; // partially freestanding
 
  template<class T, size_t N>
  constexpr bool operator==(const array<T, N>& x, const array<T, N>& y);
  template<class T, size_t N>
  constexpr /*synth-three-way-result*/<T> operator<=>(const array<T, N>& x,
                                                      const array<T, N>& y);
 
  // specialized algorithms
  template<class T, size_t N>
  constexpr void swap(array<T, N>& x, array<T, N>& y) noexcept(noexcept(x.swap(y)));
 
  // array creation functions
  template<class T, size_t N>
  constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]);
  template<class T, size_t N>
  constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]);
 
  // tuple interface
  template<class T>
  struct tuple_size;
  template<size_t I, class T>
  struct tuple_element;
  template<class T, size_t N>
  struct tuple_size<array<T, N>>;
  template<size_t I, class T, size_t N>
  struct tuple_element<I, array<T, N>>;
  template<size_t I, class T, size_t N>
  constexpr T& get(array<T, N>&) noexcept;
  template<size_t I, class T, size_t N>
  constexpr T&& get(array<T, N>&&) noexcept;
  template<size_t I, class T, size_t N>
  constexpr const T& get(const array<T, N>&) noexcept;
  template<size_t I, class T, size_t N>
  constexpr const T&& get(const array<T, N>&&) noexcept;
}

[编辑] 类模板std::array

namespace std {
  template<class T, size_t N>
  struct array
  {
    // types
    using value_type             = T;
    using pointer                = T*;
    using const_pointer          = const T*;
    using reference              = T&;
    using const_reference        = const T&;
    using size_type              = size_t;
    using difference_type        = ptrdiff_t;
    using iterator               = /* implementation-defined */;
    using const_iterator         = /* implementation-defined */;
    using reverse_iterator       = std::reverse_iterator<iterator>;
    using const_reverse_iterator = std::reverse_iterator<const_iterator>;
 
    // no explicit construct/copy/destroy for aggregate type
 
    constexpr void fill(const T& u);
    constexpr void swap(array&) noexcept(is_nothrow_swappable_v<T>);
 
    // iterators
    constexpr iterator begin() noexcept;
    constexpr const_iterator begin() const noexcept;
    constexpr iterator end() noexcept;
    constexpr const_iterator end() const noexcept;
 
    constexpr reverse_iterator rbegin() noexcept;
    constexpr const_reverse_iterator rbegin() const noexcept;
    constexpr reverse_iterator rend() noexcept;
    constexpr const_reverse_iterator rend() const noexcept;
 
    constexpr const_iterator cbegin() const noexcept;
    constexpr const_iterator cend() const noexcept;
    constexpr const_reverse_iterator crbegin() const noexcept;
    constexpr const_reverse_iterator crend() const noexcept;
 
    // capacity
    constexpr bool empty() const noexcept;
    constexpr size_type size() const noexcept;
    constexpr size_type max_size() const noexcept;
 
    // element access
    constexpr reference operator[](size_type n);
    constexpr const_reference operator[](size_type n) const;
    constexpr reference at(size_type n);             // freestanding-deleted
    constexpr const_reference at(size_type n) const; // freestanding-deleted
    constexpr reference front();
    constexpr const_reference front() const;
    constexpr reference back();
    constexpr const_reference back() const;
 
    constexpr T* data() noexcept;
    constexpr const T* data() const noexcept;
  };
 
  template<class T, class... U>
  array(T, U...) -> array<T, 1 + sizeof...(U)>;
}