Cpp Standard Library 简明教程
C++ Library - <array>
Introduction
数组是定长的序列容器。容器是保存相同类型数据的对象。序列容器将元素严格存储在线性序列中。
Arrays are sequence container of fixed size. Container is a objects that holds data of same type. Sequence containers store elements strictly in linear sequence.
容器类使用隐式构造函数静态分配所需的内存。内存是在编译时分配的,因此数组大小在运行时无法缩小或扩展。数组内的所有元素都位于连续的内存位置。
The container class uses implicit constructor to allocate required memory statically. Memory is allocated at the compile time, hence array size cannot shrink or expand at runtime. All elements inside array are located at contiguous memory locations.
Definition
以下是 <array> 头文件中的 std::array 定义。
Below is definition of std::array from <array> header file.
template < class T, size_t N >
class array;
Parameters
-
T − Type of the element contained. T may be substituted by any other data type including user-defined type.
-
N − Size of the array. Zero sized arrays are also valid. In that case array.begin() and array.end() points to same location. But behavior of calling front() or back() is undefined.
Member types
以下成员类型可作为参数或返回类型,由成员函数使用。
Following member types can be used as parameters or return type by member functions.
Sr.No. |
Member types |
Definition |
1 |
value_type |
T (First parameter of the template) |
2 |
reference |
value_type& |
3 |
const_reference |
const value_type& |
4 |
pointer |
value_type* |
5 |
const_pointer |
const value_type* |
6 |
iterator |
a random access iterator to value_type |
7 |
const_iterator |
a random access iterator to const value_type |
8 |
reverse_iterator |
std::reverse_iterator <iterator> |
9 |
const_reverse_iterator |
std::reverse_iterator <const_iterator> |
10 |
size_type |
size_t |
11 |
difference_type |
ptrdiff_t |
Functions from <array>
以下是 <array> 头文件中的所有方法列表。
Below is list of all methods from <array> header.
Member functions
Sr.No. |
Method & Description |
1 |
array::atReturns a reference to the element present at location N in given array container. |
2 |
array::backReturns a reference to the last element of the array container. |
3 |
array::beginReturns an iterator which points to the start of the array. |
4 |
array::cbeginReturns a constant iterator which points to the start of the array. |
5 |
array::cendReturns a constant iterator which points to the past-end element of array. |
6 |
array::crbeginReturns a constant reverse iterator pointing to the last element of the array. |
7 |
array::crendReturns a constant reverse iterator which points to the past-end. |
8 |
array::dataReturn a pointer pointing to the first element of the array container. |
9 |
array::emptyTests whether size of array is zero or not. |
10 |
array::endReturns an iterator which points to the past-end element of array. |
11 |
array::fillSets given value to all elements of array. |
12 |
array::frontReturns a reference to the first element of the array container. |
13 |
array::max_sizeReturns the maximum number of elements that can be held by array container. |
14 |
[role="bare"]../cpp_standard_library/cpp_array_operator.html[array::operator[\]]Returns a reference to the element present at location N in a given array container. |
15 |
array::rbeginReturns a reverse iterator pointing to the last element of the array. |
16 |
array::rendReturns a reverse iterator which points to the theoretical element preceding to first element of the array. |
17 |
array::sizeReturns the number of elements present in the array. |
18 |
array::swapSwap the contents of the two array. |
Non-member overloaded functions
Sr.No. |
Method & Description |
1 |
get(array)Returns reference to the Ith element of the array container. |
2 |
bool operator==Tests whether two containers are identical or not |
3 |
bool operator!=Tests whether two containers are identical or not |
4 |
bool operator<Tests whether first array container is less than second or not. |
5 |
bool operator⇐Tests whether first array container is less than or equal to second or not. |
6 |
bool operator>Tests whether first array container is greater than second or not. |
7 |
bool operator>=Tests whether first array container is greater than or equal to second or not. |
Non-member specilization functions
Sr.No. |
Method & Description |
1 |
tuple_element(array)Provides compile-type indexed access to the type of the elements of the array using tuple-like interface. |
2 |
tuple_size(array)Returns the total number of elements present in the container. |