Namespaces
Variants
Views
Actions

std::array

From cppreference.com
Defined in header <array>
template<

    class T,
    size_t N

> struct array;
(since C++11)

array is a container that encapsulates constant size arrays.

This struct is designed to provide the benefits of a standard container (an array knows its own size, supports assignment, random access iterators, etc.) while still providing the aggregate type semantics of C-style arrays.

There is a special case for a zero-length array (N == 0). In that case, array.begin() == array.end(), which is some unique value. The effect of calling front() or back() on a zero-sized array is undefined.

array is an aggregate (it has no constructors and no private or protected members), which allows it to use aggregate-initialization.

Contents

[edit] Member types

Member type Definition
value_type T [edit]
size_type size_t [edit]
difference_type ptrdiff_t [edit]
reference value_type& [edit]
const_reference const value_type& [edit]
pointer T*[edit]
const_pointer const T*[edit]
iterator RandomAccessIterator [edit]
const_iterator Constant random access iterator [edit]
reverse_iterator std::reverse_iterator<iterator> [edit]
const_reverse_iterator std::reverse_iterator<const_iterator> [edit]

[edit] Member functions

Element access
access specified element with bounds checking
(public member function) [edit]
access specified element
(public member function) [edit]
access the first element
(public member function) [edit]
access the last element
(public member function) [edit]
(C++11)
direct access to the underlying array
(public member function) [edit]
Iterators
returns an iterator to the beginning
(public member function) [edit]
returns an iterator to the end
(public member function) [edit]
returns a reverse iterator to the beginning
(public member function) [edit]
returns a reverse iterator to the end
(public member function) [edit]
Capacity
checks whether the container is empty
(public member function) [edit]
returns the number of elements
(public member function) [edit]
returns the maximum possible number of elements
(public member function) [edit]
Operations
fill the container with specified value
(public member function) [edit]
swaps the contents
(public member function) [edit]

[edit] Non-member functions

lexicographically compares the values in the array
(function template) [edit]
accesses an element of an array
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]

[edit] Helper classes

obtains the size of an array
(class template specialization) [edit]
obtains the type of the elements of array
(class template specialization) [edit]

[edit] Example

#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
    std::array<int, 3> a2 = {1, 2, 3}; // except 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, " "));
 
    // ranged for loop is supported
    for(auto& s: a3)
        std::cout << s << ' ';
}

Output:

3 2 1 a b