Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/array"

From cppreference.com
< cpp‎ | container
m (Text replace - "{{tdcl" to "{{dcl")
m (See also: +deque)
 
(110 intermediate revisions by 51 users not shown)
Line 1: Line 1:
 
{{cpp/title|array}}
 
{{cpp/title|array}}
{{cpp/container/array/sidebar}}
+
{{cpp/container/array/navbar}}
{{ddcl | header=array | notes={{mark since c++11}} |
+
{{ddcl|header=array|since=c++11|
template<  
+
template<
     class T,  
+
     class T,
     size_t N  
+
     std::size_t N
 
> struct array;
 
> struct array;
 
}}
 
}}
  
<code>array</code> is a container that encapsulates constant size arrays.
+
{{tt|std::array}} is a container that encapsulates fixed size arrays.
  
This struct is designed to provide the benefits of a standard container (an {{tt|array}} knows its own size, supports assignment, random access iterators, etc.)
+
This container is an aggregate type with the same semantics as a struct holding a [[cpp/language/array|C-style array]] {{c|T[N]}} as its only non-static data member. Unlike a C-style array, it doesn't decay to {{c|T*}} automatically. As an aggregate type, it can be initialized with [[cpp/language/aggregate initialization|aggregate-initialization]] given at most {{tt|N}} initializers that are convertible to {{tt|T}}: {{c|1=std::array<int, 3> a = {1, 2, 3};}}.
while still providing the aggregate type semantics of C-style arrays.
+
  
There is a special case for a zero-length array (<code>N == 0</code>). In that case, <code>array.begin() == array.end()</code>, which is some unique value.  The effect of calling <code>front()</code> or <code>back()</code> on a zero-sized array is undefined.
+
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.
  
<code>array</code> is a class aggregate which allows it to use aggregate-initialization.
+
{{tt|std::array}} satisfies the requirements of {{named req|Container}} and {{named req|ReversibleContainer}} except that default-constructed array is not empty and that the complexity of swapping is linear, {{rev inl|since=c++17|satisfies the requirements of {{named req|ContiguousContainer}},}} and partially satisfies the requirements of {{named req|SequenceContainer}}.
 +
 
 +
There is a special case for a zero-length array ({{tt|1=N == 0}}). In that case, {{c|
 +
1=array.begin() == array.end()}}, which is some unique value. The effect of calling {{c|front()}} or {{c|back()}} on a zero-sized array is undefined.
 +
 
 +
An array can also be used as a tuple of {{tt|N}} elements of the same type.
 +
 
 +
===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 {{rl|swap}}, the iterator will continue to point to the same array element, and will thus change its value.
 +
 
 +
===Template parameters===
 +
{{par begin}}
 +
{{par|T|element type<!--; must be a complete object type that is not an abstract class type.--> Must be {{named req|MoveConstructible}} and {{named req|MoveAssignable}}.}}
 +
{{par|N|the number of elements in the array or {{c|0}}.}}
 +
{{par end}}
 +
{{todo|Complete the descriptions of template parameters.}}
  
 
===Member types===
 
===Member types===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{dcl list template | cpp/container/tdcl list value_type | array}}
+
{{dsc inc|cpp/container/dsc value_type|array}}
{{dcl list template | cpp/container/tdcl list size_type | array}}
+
{{dsc inc|cpp/container/dsc size_type|array}}
{{dcl list template | cpp/container/tdcl list difference_type | array}}
+
{{dsc inc|cpp/container/dsc difference_type|array}}
{{dcl list template | cpp/container/tdcl list reference | array}}
+
{{dsc inc|cpp/container/dsc reference|array}}
{{dcl list template | cpp/container/tdcl list const_reference | array}}
+
{{dsc inc|cpp/container/dsc const_reference|array}}
{{dcl list template | cpp/container/tdcl list pointer | array}}
+
{{dsc inc|cpp/container/dsc pointer|array}}
{{dcl list template | cpp/container/tdcl list const_pointer | array}}
+
{{dsc inc|cpp/container/dsc const_pointer|array}}
{{dcl list template | cpp/container/tdcl list iterator | array}}
+
{{dsc inc|cpp/container/dsc iterator|array}}
{{dcl list template | cpp/container/tdcl list const_iterator | array}}
+
{{dsc inc|cpp/container/dsc const_iterator|array}}
{{dcl list template | cpp/container/tdcl list reverse_iterator | array}}
+
{{dsc inc|cpp/container/dsc reverse_iterator|array}}
{{dcl list template | cpp/container/tdcl list const_reverse_iterator | array}}
+
{{dsc inc|cpp/container/dsc const_reverse_iterator|array}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list h2 | Element access}}
+
{{dsc h2|Implicitly-defined member functions}}
{{dcl list template | cpp/container/dcl list at | array}}
+
{{dsc mem ctor|nolink=true|notes={{mark implicit}}|initializes the array following the rules of [[cpp/language/aggregate initialization|aggregate initialization]] (note that default initialization may result in indeterminate values for non-class {{tt|T}})}}
{{dcl list template | cpp/container/dcl list operator_at | array}}
+
{{dsc mem dtor|nolink=true|notes={{mark implicit}}|destroys every element of the array}}
{{dcl list template | cpp/container/dcl list front | array}}
+
{{dsc mem fun|operator{{=}}|nolink=true|notes={{mark implicit}}|overwrites every element of the array with the corresponding element of another array}}
{{dcl list template | cpp/container/dcl list back | array}}
+
{{dcl list template | cpp/container/dcl list data | array}}
+
  
{{dcl list h2 | Iterators}}
+
{{dsc h2|Element access}}
{{dcl list template | cpp/container/dcl list begin | array}}
+
{{dsc inc|cpp/container/dsc at|array}}
{{dcl list template | cpp/container/dcl list end | array}}
+
{{dsc inc|cpp/container/dsc operator_at|array}}
{{dcl list template | cpp/container/dcl list rbegin | array}}
+
{{dsc inc|cpp/container/dsc front|array}}
{{dcl list template | cpp/container/dcl list rend | array}}
+
{{dsc inc|cpp/container/dsc back|array}}
 +
{{dsc inc|cpp/container/dsc data|array}}
  
{{dcl list h2 | Capacity}}
+
{{dsc h2|Iterators}}
{{dcl list template | cpp/container/dcl list empty | array}}
+
{{dsc inc|cpp/container/dsc begin|array}}
{{dcl list template | cpp/container/dcl list size | array}}
+
{{dsc inc|cpp/container/dsc end|array}}
{{dcl list template | cpp/container/dcl list max_size | array}}
+
{{dsc inc|cpp/container/dsc rbegin|array}}
 +
{{dsc inc|cpp/container/dsc rend|array}}
  
{{dcl list h2 | Operations}}
+
{{dsc h2|Capacity}}
{{dcl list template | cpp/container/dcl list fill | array}}
+
{{dsc inc|cpp/container/dsc empty|array}}
{{dcl list template | cpp/container/dcl list swap | array}}
+
{{dsc inc|cpp/container/dsc size|array}}
{{dcl list end}}
+
{{dsc inc|cpp/container/dsc max_size|array}}
 +
 
 +
{{dsc h2|Operations}}
 +
{{dsc inc|cpp/container/dsc fill|array}}
 +
{{dsc inc|cpp/container/dsc swap|array}}
 +
{{dsc end}}
  
 
===Non-member functions===
 
===Non-member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/container/dcl list operator_cmp | array}}
+
{{dsc inc|cpp/container/dsc operator_cmp|array}}
{{dcl list template | cpp/container/array/dcl list get}}
+
{{dsc inc|cpp/container/array/dsc get}}
{{dcl list template | cpp/container/dcl list swap2 | array}}
+
{{dsc inc|cpp/container/dsc swap2|array}}
{{dcl list end}}
+
{{dsc inc|cpp/container/array/dsc to_array}}
 +
{{dsc end}}
  
 
===Helper classes===
 
===Helper classes===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/container/array/dcl list tuple_size}}
+
{{dsc inc|cpp/container/array/dsc tuple_size}}
{{dcl list template | cpp/container/array/dcl list tuple_element}}
+
{{dsc inc|cpp/container/array/dsc tuple_element}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
{{rrev|since=c++17|
 +
==={{rl|deduction guides|Deduction guides}}===
 +
}}
 +
 
 +
===Example===
 +
{{example
 +
|code=
 +
#include <algorithm>
 +
#include <array>
 +
#include <iostream>
 +
#include <iterator>
 +
#include <string>
 +
 
 +
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 =
 +
 
 +
    // Container operations are supported
 +
    std::sort(a1.begin(), a1.end());
 +
    std::ranges::reverse_copy(a2, std::ostream_iterator<int>(std::cout, " "));
 +
    std::cout << '\n';
 +
 
 +
    // Ranged for loop is supported
 +
    std::array<std::string, 2> a3{"E", "\u018E"};
 +
    for (const auto& s : a3)
 +
        std::cout << s << ' ';
 +
    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>
 +
 
 +
    // Behavior of unspecified elements is the same as with built-in arrays
 +
    [[maybe_unused]] std::array<int, 2> a5; // No list init, a5[0] and a5[1]
 +
                                            // are default initialized
 +
    [[maybe_unused]] std::array<int, 2> a6{}; // List init, both elements are value
 +
                                              // initialized, a6[0] = a6[1] = 0
 +
    [[maybe_unused]] std::array<int, 2> a7{1}; // List init, unspecified element is value
 +
                                              // initialized, a7[0] = 1, a7[1] = 0
 +
}
 +
|output=
 +
3 2 1
 +
E Ǝ
 +
}}
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/container/dsc inplace_vector}}
 +
{{dsc inc|cpp/container/dsc vector}}
 +
{{dsc inc|cpp/container/dsc deque}}
 +
{{dsc inc|cpp/experimental/dsc make_array}}
 +
{{dsc end}}
  
[[es:cpp/container/array]]
+
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}

Latest revision as of 21:20, 2 August 2024

 
 
 
 
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

[edit] 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.

[edit] Template parameters

T - element type Must be MoveConstructible and MoveAssignable.
N - the number of elements in the array or 0.

[edit] 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]

[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]

[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]

[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)

[edit] Example

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string>
 
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 =
 
    // Container operations are supported
    std::sort(a1.begin(), a1.end());
    std::ranges::reverse_copy(a2, std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
 
    // Ranged for loop is supported
    std::array<std::string, 2> a3{"E", "\u018E"};
    for (const auto& s : a3)
        std::cout << s << ' ';
    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>
 
    // Behavior of unspecified elements is the same as with built-in arrays
    [[maybe_unused]] std::array<int, 2> a5; // No list init, a5[0] and a5[1]
                                            // are default initialized
    [[maybe_unused]] std::array<int, 2> a6{}; // List init, both elements are value
                                              // initialized, a6[0] = a6[1] = 0
    [[maybe_unused]] std::array<int, 2> a7{1}; // List init, unspecified element is value
                                               // initialized, a7[0] = 1, a7[1] = 0
}

Output:

3 2 1
E Ǝ

[edit] See also

dynamically-resizable, fixed capacity, inplace contiguous array
(class template) [edit]
dynamic contiguous array
(class template) [edit]
double-ended queue
(class template) [edit]
(library fundamentals TS v2)
creates a std::array object whose size and optionally element type are deduced from the arguments
(function template) [edit]