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.
Below is definition of std::array from <array> header file.
template < class T, size_t N > class array;
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.
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 |
Below is list of all methods from <array> header.
Sr.No. | Method & Description |
---|---|
1 | array::at
Returns a reference to the element present at location N in given array container. |
2 | array::back
Returns a reference to the last element of the array container. |
3 | array::begin
Returns an iterator which points to the start of the array. |
4 | array::cbegin
Returns a constant iterator which points to the start of the array. |
5 | array::cend
Returns a constant iterator which points to the past-end element of array. |
6 | array::crbegin
Returns a constant reverse iterator pointing to the last element of the array. |
7 | array::crend
Returns a constant reverse iterator which points to the past-end. |
8 | array::data
Return a pointer pointing to the first element of the array container. |
9 | array::empty
Tests whether size of array is zero or not. |
10 | array::end
Returns an iterator which points to the past-end element of array. |
11 | array::fill
Sets given value to all elements of array. |
12 | array::front
Returns a reference to the first element of the array container. |
13 | array::max_size
Returns the maximum number of elements that can be held by array container. |
14 | array::operator[]
Returns a reference to the element present at location N in a given array container. |
15 | array::rbegin
Returns a reverse iterator pointing to the last element of the array. |
16 | array::rend
Returns a reverse iterator which points to the theoretical element preceding to first element of the array. |
17 | array::size
Returns the number of elements present in the array. |
18 | array::swap
Swap the contents of the two array. |
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. |
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. |