首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::array

Defined in header <array>

template< class T, std::size_t N > struct array;

(since C++11)

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

此容器是一个聚合类型,其语义与包含C型阵列T[N]作为它唯一的非静态数据成员。与C型数组不同,它不会将%27T衰减到T*自动的。作为聚合类型,可以使用聚合-初始化给最多N可转换为T*std::array<int, 3> a = {1,2,3};...

该结构将C样式数组的性能和可访问性与标准容器的优点结合在一起,例如了解其自身大小、支持赋值、随机访问迭代器等。

std::array满足…的要求ContainerReversibleContainer除非默认构造的数组不是空的,而且交换的复杂性是线性的,满足ContiguousContainer%28自C++17%29以来,部分满足SequenceContainer...

对于零长度数组%28有一个特例。N == 029%。在这种情况下,array.begin() == array.end(),这是一些独特的价值。呼叫的效果front()back()在零大小数组上未定义.

数组也可用作N相同类型的元素。

迭代器失效

通常,数组的迭代器在数组的整个生命周期内都不会失效。然而,人们应该注意到,在互换,迭代器将继续指向相同的数组元素,从而更改其值。

成员类型

Member type

Definition

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

RandomAccessIterator and LiteralType (since C++17)

const_iterator

Constant random access iterator and LiteralType (since C++17)

reverse_iterator

std::reverse_iterator<iterator>

const_reverse_iterator

std::reverse_iterator<const_iterator>

成员函数

隐式定义成员函数

*。

%28构造函数%29%28隐式声明%29按照聚合初始化规则初始化数组注意默认初始化可能导致非类T%29%28公共成员函数%29的不确定性值

%28析构函数%29%隐式声明%29销毁数组%28公共成员函数%29的每个元素

运算符=%28隐式声明的%29用另一个数组%28公共成员函数的对应元素覆盖数组的每个元素

元素存取

在访问指定元素时,使用边界检查%28公共成员函数%29

操作者。[]访问指定元素%28公共成员函数%29

前端访问第一个元素%28公共成员函数%29

返回访问最后一个元素%28公共成员函数%29

数据直接访问基础数组%28公共成员函数%29

迭代器

BEGINCBEGIN将迭代器返回到开头%28的公共成员函数%29

End cend将迭代器返回到End%28公共成员函数%29

将反向迭代器返回到开头%28的公共成员函数%29

rend crend将反向迭代器返回到End%28公共成员函数%29

容量

空检查容器是否为空%28公共成员函数%29

Size返回元素数%28公共成员函数%29

马克斯[医]Size返回元素的最大可能数%28公共成员函数%29

操作

使用指定值%28公共成员函数%29填充容器

交换交换内容%28公共成员函数%29

非会员职能

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the array (function template)

std::get(std::array)

accesses an element of an array (function template)

std::swap(std::array) (C++11)

specializes the std::swap algorithm (function template)

帮助者类

std::tuple_size<std::array>

obtains the size of an array (class template specialization)

std::tuple_element<std::array>

obtains the type of the elements of array (class template specialization)

二次

代码语言:javascript
复制
#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <array>
 
int main()
{
    // construction uses aggregate initialization
    std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 (not in C++14)
    std::array<int, 3> a2 = {1, 2, 3};  // never required after =
    std::array<std::string, 2> a3 = { std::string("a"), "b" };
 
    // container operations are supported
    std::sort(a1.begin(), a1.end());
    std::reverse_copy(a2.begin(), a2.end(), 
                      std::ostream_iterator<int>(std::cout, " "));
 
    std::cout << '\n';
 
    // ranged for loop is supported
    for(const auto& s: a3)
        std::cout << s << ' ';
}

二次

产出:

二次

代码语言:javascript
复制
3 2 1 
a b

二次

另见

make_array

Creates a std::array object whose size and optionally element type are deduced from the arguments (function template)

to_array

Creates a std::array object from a built-in array (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券