Namespaces
Variants
Views
Actions

std::array

From cppreference.com
< cpp‎ | container
Revision as of 18:39, 31 May 2013 by P12bot (Talk | contribs)

 
 
 
 
Defined in header <array>
template<

    class T,
    std::size_t N

> struct array;
(since C++11)

std::array is a container that encapsulates constant size arrays.

This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. It can be initialized with aggregate-initialization, given at most N initializers that are convertible to T: std::array<int, 3> a = {1,2,3};

The struct combines the performance and accessibility of a C-style array with the benefits of a standard container, such as knowing its own size, supporting assignment, random access iterators, etc.

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.

An array can also be used as a tuple of N elements of the same type.

Contents

Member types

Template:cpp/container/dcl list value typeTemplate:cpp/container/dcl list size typeTemplate:cpp/container/dcl list difference typeTemplate:cpp/container/dcl list referenceTemplate:cpp/container/dcl list const referenceTemplate:cpp/container/dcl list pointerTemplate:cpp/container/dcl list const pointerTemplate:cpp/container/dcl list iteratorTemplate:cpp/container/dcl list const iteratorTemplate:cpp/container/dcl list reverse iteratorTemplate:cpp/container/dcl list const reverse iterator
Member type Definition

Member functions

Template:cpp/container/dcl list atTemplate:cpp/container/dcl list operator atTemplate:cpp/container/dcl list frontTemplate:cpp/container/dcl list backTemplate:cpp/container/dcl list dataTemplate:cpp/container/dcl list beginTemplate:cpp/container/dcl list endTemplate:cpp/container/dcl list rbeginTemplate:cpp/container/dcl list rendTemplate:cpp/container/dcl list emptyTemplate:cpp/container/dcl list sizeTemplate:cpp/container/dcl list max sizeTemplate:cpp/container/dcl list fillTemplate:cpp/container/dcl list swap
Implicitly-defined member functions
(constructor)
(implicitly declared)
default-constructs or copy-constructs every element of the array
(public member function)
(destructor)
(implicitly declared)
destroys every element of the array
(public member function)
operator=
(implicitly declared)
overwrites every element of the array with the corresponding element of another array
(public member function)
Element access
Iterators
Capacity
Operations

Non-member functions

Template:cpp/container/dcl list operator cmpTemplate:cpp/container/array/dcl list getTemplate:cpp/container/dcl list swap2

Helper classes

Template:cpp/container/array/dcl list tuple sizeTemplate:cpp/container/array/dcl list tuple element

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