Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/array"

From cppreference.com
< cpp‎ | container
(Undo revision 140122 by Space Mission (talk). Already listed as a non-member function)
(Added See also items and added empty initialization example see c/language/initialization#Empty initialization)
Line 117: Line 117:
 
      
 
      
 
     // ranged for loop is supported
 
     // ranged for loop is supported
     for(const auto& s: a3)
+
     for(const auto& s : a3)
 
         std::cout << s << ' ';
 
         std::cout << s << ' ';
 +
    std::cout << '\n';
 +
   
 +
    // Empty initialization
 +
    std::array<int, 3> z1 = {};        // = {0, 0, 0}
 +
    std::array<double, 3> z2 = {1.1};  // = {1.1, 0.0, 0.0}
 +
 +
    for(auto n : z1)
 +
        std::cout << n << ' ';
 +
    std::cout << '\n';
 +
    for(auto n : z2)
 +
        std::cout << n << ' ';
  
 
     // deduction guide for array creation (since C++17)
 
     // deduction guide for array creation (since C++17)
Line 126: Line 137:
 
3 2 1  
 
3 2 1  
 
a b  
 
a b  
 +
0 0 0
 +
1.1 0 0
 
}}
 
}}
  
Line 131: Line 144:
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc inc | cpp/experimental/dsc make array}}
 
{{dsc inc | cpp/experimental/dsc make array}}
 +
{{dsc see cpp | cpp/language/array | Array declaration | nomono=true}}
 +
{{dsc see c | c/language/array | Array declaration | nomono=true}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}

Revision as of 18:21, 1 September 2022

 
 
 
 
Defined in header <array>
template<

    class T,
    std::size_t N

> struct array;
(since C++11)

std::array is a container that encapsulates fixed 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. Unlike a C-style array, it doesn't decay to T* automatically. As an aggregate type, 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.

std::array satisfies the requirements of Container and ReversibleContainer except that default-constructed array is not empty and that the complexity of swapping is linear, satisfies the requirements of ContiguousContainer,(since C++17) and partially satisfies the requirements of SequenceContainer.

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

Iterator invalidation

As a rule, iterators to an array are never invalidated throughout the lifetime of the array. One should take note, however, that during swap, the iterator will continue to point to the same array element, and will thus change its value.

Member types

Member type Definition
value_type T[edit]
size_type std::size_t[edit]
difference_type std::ptrdiff_t[edit]
reference value_type&[edit]
const_reference const value_type&[edit]
pointer value_type*[edit]
const_pointer const value_type*[edit]
iterator

LegacyRandomAccessIterator and LegacyContiguousIterator to value_type

(until C++17)

LegacyRandomAccessIterator and LegacyContiguousIterator that is a LiteralType to value_type

(since C++17)
(until C++20)

LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to value_type

(since C++20)
[edit]
const_iterator

LegacyRandomAccessIterator and LegacyContiguousIterator to const value_type

(until C++17)

LegacyRandomAccessIterator and LegacyContiguousIterator that is a LiteralType to const value_type

(since C++17)
(until C++20)

LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to const value_type

(since C++20)
[edit]
reverse_iterator std::reverse_iterator<iterator>[edit]
const_reverse_iterator std::reverse_iterator<const_iterator>[edit]

Member functions

Implicitly-defined member functions
(constructor)
(implicitly declared)
initializes the array following the rules of aggregate initialization (note that default initialization may result in indeterminate values for non-class T)
(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
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]
direct access to the underlying contiguous storage
(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]

Non-member functions

(C++11)(C++11)(removed in C++20)(C++11)(removed in C++20)(C++11)(removed in C++20)(C++11)(removed in C++20)(C++11)(removed in C++20)(C++20)
lexicographically compares the values of two arrays
(function template) [edit]
accesses an element of an array
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
(C++20)
creates a std::array object from a built-in array
(function template) [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]

Deduction guides(since C++17)

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 in C++11 prior to
                                        // the CWG 1270 revision (not needed in C++11
                                        // after the revision and in C++14 and beyond)
 
    std::array<int, 3> a2 = {1, 2, 3};  // double braces 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 << ' ';
    std::cout << '\n';
 
    // Empty initialization
    std::array<int, 3> z1 = {};         // = {0, 0, 0}
    std::array<double, 3> z2 = {1.1};   // = {1.1, 0.0, 0.0}
 
    for(auto n : z1)
        std::cout << n << ' ';
    std::cout << '\n';
    for(auto n : z2)
        std::cout << n << ' ';
 
    // deduction guide for array creation (since C++17)
    [[maybe_unused]] std::array a4{3.0, 1.0, 4.0};  // -> std::array<double, 3>
}

Output:

3 2 1 
a b 
0 0 0 
1.1 0 0

See also

(library fundamentals TS v2)
creates a std::array object whose size and optionally element type are deduced from the arguments
(function template) [edit]
C++ documentation for Array declaration
C documentation for Array declaration