std::array介绍

template < class T, size_t N > class array;
Array class
Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.

Internally, an array does not keep any data other than the elements it contains (not even its size, which is a template parameter, fixed on compile time). It is as efficient in terms of storage size as an ordinary array declared with the language's bracket syntax ( []). This class merely adds a layer of member and global functions to it, so that arrays can be used as standard containers.

Unlike the other standard containers, arrays have a fixed size and do not manage the allocation of its elements through an allocator: they are an aggregate type encapsulating a fixed-size array of elements. Therefore, they cannot be expanded or contracted dynamically (see  vector for a similar container that can be expanded).

Zero-sized arrays are valid, but they should not be dereferenced (members  frontback, and  data).

Unlike with the other containers in the Standard Library, swapping two array containers is a linear operation that involves swapping all the elements in the ranges individually, which generally is a considerably less efficient operation. On the other side, this allows the iterators to elements in both containers to keep their original container association.

Another unique feature of array containers is that they can be treated as  tuple objects: The  <array> header overloads the get function to access the elements of the array as if it was a  tuple, as well as specialized  tuple_size and  tuple_elementtypes.

Container properties

Sequence
Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.
Contiguous storage
The elements are stored in contiguous memory locations, allowing constant time random access to elements. Pointers to an element can be offset to access other elements.
Fixed-size aggregate
The container uses implicit constructors and destructors to allocate the required space statically. Its size is compile-time constant. No memory or time overhead.

Template parameters

T
Type of the elements contained.
Aliased as member type  array::value_type.
N
Size of the array, in terms of number of elements.
In the reference for the  array member functions, these same names are assumed for the template parameters.

Member types

The following aliases are member types of  array. They are widely used as parameter and return types by member functions:

member typedefinitionnotes
value_typeThe first template parameter (T) 
referencevalue_type& 
const_referenceconst value_type& 
pointervalue_type* 
const_pointerconst value_type* 
iteratorrandom access iterator to value_typeconvertible to const_iterator
const_iteratorrandom access iterator to const value_type 
reverse_iteratorreverse_iterator<iterator> 
const_reverse_iteratorreverse_iterator<const_iterator> 
size_typesize_tunsigned integral type
difference_typeptrdiff_tsigned integral type

Member functions

Iterators

Capacity

Element access

Modifiers

Non-member function overloads


Non-member class specializations

std::array和std::vector都是C++中的容器,用于存储和管理一系列的元素。它们有一些相似之处,但也有一些不同之处。 1. std::arraystd::array是一个固定大小的数组,它在编译时就确定了大小。它的大小是固定的,不能动态改变。std::array的元素在内存中是连续存储的,可以通过索引访问元素。以下是std::array的一些常用操作[^1]: - 创建一个空的std::array对象:std::array<T, N> arr; - 获取std::array的大小:arr.size(); - 访问std::array中的元素:arr[i]或arr.at(i); - 清空std::array中的所有元素:arr.fill(value); 2. std::vector: std::vector是一个动态数组,它的大小可以在运行时动态改变。std::vector的元素在内存中也是连续存储的,可以通过索引访问元素。以下是std::vector的一些常用操作: - 创建一个空的std::vector对象:std::vector<T> vec; - 创建一个指定大小的std::vector对象:std::vector<T> vec(n); - 在std::vector的末尾添加元素:vec.push_back(value); - 获取std::vector的大小:vec.size(); - 清空std::vector中的所有元素:vec.clear(); - 删除std::vector中的最后一个元素:vec.pop_back(); - 删除std::vector中的指定元素:vec.erase(vec.begin() + i); - 迭代访问std::vector中的元素:for (auto it = vec.begin(); it != vec.end(); ++it) { ... } std::array适用于大小固定且不需要频繁创建销毁的情况,而std::vector适用于大小不确定且需要频繁创建销毁的情况。在性能方面,std::array在大部分情况下与std::vector相比没有明显的差距。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值