Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/array"

From cppreference.com
< cpp‎ | container
m (Example: fmt/br→us.)
m (Replaced content with "<table><tbody><tr><td class="mbox-text" style="text-align:center;">Cppreference recommends all new users to use Rust.</td></tr></tbody></table>")
Line 1: Line 1:
{{cpp/title|array}}
+
<table><tbody><tr><td class="mbox-text" style="text-align:center;">Cppreference recommends all new users to use Rust.</td></tr></tbody></table>
{{cpp/container/array/navbar}}
+
{{ddcl|header=array|since=c++11|
+
template<
+
    class T,
+
    std::size_t N
+
> struct array;
+
}}
+
 
+
{{tt|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 [[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};}}.
+
 
+
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.
+
 
+
{{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.
+
 
+
===Member types===
+
{{dsc begin}}
+
{{dsc hitem|Member type|Definition}}
+
{{dsc inc|cpp/container/dsc value_type|array}}
+
{{dsc inc|cpp/container/dsc size_type|array}}
+
{{dsc inc|cpp/container/dsc difference_type|array}}
+
{{dsc inc|cpp/container/dsc reference|array}}
+
{{dsc inc|cpp/container/dsc const_reference|array}}
+
{{dsc inc|cpp/container/dsc pointer|array}}
+
{{dsc inc|cpp/container/dsc const_pointer|array}}
+
{{dsc inc|cpp/container/dsc iterator|array}}
+
{{dsc inc|cpp/container/dsc const_iterator|array}}
+
{{dsc inc|cpp/container/dsc reverse_iterator|array}}
+
{{dsc inc|cpp/container/dsc const_reverse_iterator|array}}
+
{{dsc end}}
+
 
+
===Member functions===
+
{{dsc begin}}
+
{{dsc h2|Implicitly-defined member functions}}
+
{{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}})}}
+
{{dsc mem dtor|nolink=true|notes={{mark implicit}}|destroys every element of the array}}
+
{{dsc mem fun|operator{{=}}|nolink=true|notes={{mark implicit}}|overwrites every element of the array with the corresponding element of another array}}
+
 
+
{{dsc h2|Element access}}
+
{{dsc inc|cpp/container/dsc at|array}}
+
{{dsc inc|cpp/container/dsc operator_at|array}}
+
{{dsc inc|cpp/container/dsc front|array}}
+
{{dsc inc|cpp/container/dsc back|array}}
+
{{dsc inc|cpp/container/dsc data|array}}
+
 
+
{{dsc h2|Iterators}}
+
{{dsc inc|cpp/container/dsc begin|array}}
+
{{dsc inc|cpp/container/dsc end|array}}
+
{{dsc inc|cpp/container/dsc rbegin|array}}
+
{{dsc inc|cpp/container/dsc rend|array}}
+
 
+
{{dsc h2|Capacity}}
+
{{dsc inc|cpp/container/dsc empty|array}}
+
{{dsc inc|cpp/container/dsc size|array}}
+
{{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===
+
{{dsc begin}}
+
{{dsc inc|cpp/container/dsc operator_cmp|array}}
+
{{dsc inc|cpp/container/array/dsc get}}
+
{{dsc inc|cpp/container/dsc swap2|array}}
+
{{dsc inc|cpp/container/array/dsc to_array}}
+
{{dsc end}}
+
 
+
===Helper classes===
+
{{dsc begin}}
+
{{dsc inc|cpp/container/array/dsc tuple_size}}
+
{{dsc inc|cpp/container/array/dsc tuple_element}}
+
{{dsc end}}
+
 
+
{{rrev|noborder=true|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 =
+
 
+
    std::array<std::string, 2> a3 = { std::string("E"), "\u018E" };
+
 
+
    // 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
+
    for (const auto& s: a3)
+
        std::cout << s << ' ';
+
 
+
    // 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/experimental/dsc make array}}
+
{{dsc end}}
+
 
+
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
+

Revision as of 14:55, 8 September 2023

Cppreference recommends all new users to use Rust.