C++ Standard Library C++ STL Library

C++ - <array>



C++ <array>

An array is a sequence container that stores specific number of elements of same data type. Unlike vector, the size of an array is fixed. It stores data strictly in linear sequence.

Array stores elements in contiguous memory location which enables direct access of any elements using operator []. It uses implicit constructor to allocate required memory statically at compile time, hence its size cannot shrink or expand at runtime.

Syntax

template < class T, size_t N > class array; 

Parameters

T Type of the elements stored in the container.
N Size of the array in terms of number of elements.

Member Types

Member typesDefinition
value_type T (First template parameter)
reference value_type&
const_reference const value_type&
pointer value_type*
const_pointer value_type*
iterator a random access iterator to value_type
const_iterator a random access iterator to const value_type
reverse_iterator reverse_iterator <iterator>
const_reverse_iterator reverse_iterator <const_iterator>
difference_type ptrdiff_t
size_type size_t

C++ <array> - Member Functions

The C++ array container has a number of member functions which are listed below:

Capacity

FunctionsDescription
empty() Checks whether the array is empty or not.
size() Returns the length of the array in terms of bytes.
max_size() Returns the maximum length of the array.

Element Access

FunctionsDescription
at() Access an element of the array.
operator[]() Access an element of the array.
front() Access first element of the array.
back() Access last element of the array.
data() Returns a pointer to the first element of the array.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the array.
end() Returns iterator pointing to the past-the-last element of the array.
rbegin() Returns reverse iterator to the last element of the array.
rend() Returns reverse iterator to the element preceding the first element of the array.
cbegin() Returns const_iterator pointing to the first element of the array.
cend() Returns const_iterator pointing to the past-the-last element of the array.
crbegin() Returns const_reverse_iterator to the last element of the array.
crend() Returns const_reverse_iterator to the element preceding the first element of the array.

Modifiers

FunctionsDescription
fill() Set a given value to all elements of the array.
swap() Exchanges elements between two arrays.

C++ <array> - Non-member Functions

FunctionsDescription
operator == Checks whether two arrays are equal or not.
operator != Checks whether two arrays are unequal or not.
operator < Checks whether the first array is less than the other or not.
operator > Checks whether the first array is greater than the other or not.
operator <= Checks whether the first array is less than or equal to the other or not.
operator >= Checks whether the first array is greater than or equal to the other or not.
swap() Exchanges elements between two arrays.