You are on page 1of 3

std::array

Dened in header <array>

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

(sinc e C++11)

std::arrayis a container that encaps ulates xed s ize arrays .


This container is an aggregate type with the s ame s emantics as a s truct holding a C-s tyle array T[N]
as its only non-s tatic data member. Unlike a C-s tyle array, it does n't decay to T* automatically. As an
aggregate type, it can be initialized with aggregate-initialization given at mos t Ninitializers that are
convertible to T: std::array<int, 3> a = {1,2,3};.
The s truct combines the performance and acces s ibility of a C-s tyle array with the benets of a s tandard
container, s uch as knowing its own s ize, s upporting as s ignment, random acces s iterators , etc.
std::arrays atis es the requirements of Containerand ReversibleContainerexcept that defaultcons tructed array is not empty and that the complexity of s wapping is linear, s atis es the requirements
of ContiguousContainer(sinc e C++17) and partially s atis es the requirements of SequenceContainer.
There is a s pecial cas e for a zero-length array (N == 0). In that cas e,
array.begin() == array.end(), which is s ome unique value. The eect of calling front() or
back() on a zero-s ized array is undened.
An array can als o be us ed as a tuple of Nelements of the s ame type.

Iterator invalidation
As a rule, iterators to an array are never invalidated throughout the lifetime of the array. One s hould take
note, however, that during s wap, the iterator will continue to point to the s ame array element, and will
thus change its value.

Member types
Member type

Denition

value_type

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

const_iterator

Cons tant random acces s iterator

reverse_iterator

std::reverse_iterator<iterator>

const_reverse_iterator std::reverse_iterator<const_iterator>

Member functions

Implicitly-dened member functions

initialized the array following the rules of aggregate initialization (note


that default initialization may res ult in indeterminate values for non(cons tructor) (implic itly dec lared) clas s T)
(public member func tion)

(des tructor) (implic itly dec lared)


operator=(implic itly dec lared)

des troys every element of the array


(public member func tion)

overwrites every element of the array with the corres ponding element
of another array
(public member func tion)

(public member func tion)

Element access

at
operator[]
front
back
data

acces s s pecied element with bounds checking


(public member func tion)

acces s s pecied element


(public member func tion)

acces s the rs t element


(public member func tion)

acces s the las t element


(public member func tion)

direct acces s to the underlying array


(public member func tion)

Iterators

begin
cbegin

returns an iterator to the beginning

end
cend

returns an iterator to the end

rbegin
crbegin

returns a revers e iterator to the beginning

rend
crend

returns a revers e iterator to the end

(public member func tion)


(public member func tion)
(public member func tion)
(public member func tion)

Capacity

empty
size
max_size

checks whether the container is empty


(public member func tion)

returns the number of elements


(public member func tion)

returns the maximum pos s ible number of elements


(public member func tion)

Operations

fill
swap

ll the container with s pecied value


(public member func tion)

s waps the contents


(public member func tion)

Non-member functions
operator==
operator!=
operator<
operator<=
operator>
operator>=
std::get(std::array)

lexicographically compares the values in the array


(func tion template)

acces s es an element of an array


(func tion template)

s pecializes the std::swapalgorithm


std::swap(std::array)(C++11) (func tion template)

Helper classes
std::tuple_size<std::array>

obtains the s ize of an array


(c lass template spec ializ ation)

obtains the type of the elements of array


std::tuple_element<std::array> (c lass template spec ializ ation)

Example
Run this code

#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 << ' ';
}

Output:
321
ab

See also
Creates a std::arrayobject whos e s ize and optionally element type are deduced from
make_array the arguments
(func tion template)

to_array

Creates a std::arrayobject from a built-in array


(func tion template)

Retrieved from "http://en.c ppreferenc e.c om/mwiki/index.php?title=c pp/c ontainer/array& oldid=83513"

You might also like