Difference between revisions of "cpp/container/array"
m (r2.7.3) (Robot: Adding de, fr, it, ja, pt, ru, zh) |
(better layout) |
||
Line 93: | Line 93: | ||
{ | { | ||
// construction uses aggregate initialization | // construction uses aggregate initialization | ||
− | std::array<int, 3> a1{ {1,2,3} }; | + | std::array<int, 3> a1{ {1,2,3} }; // double-braces required |
std::array<int, 3> a2 = {1, 2, 3}; // except after = | std::array<int, 3> a2 = {1, 2, 3}; // except after = | ||
std::array<std::string, 2> a3 = { {std::string("a"), "b"} }; | std::array<std::string, 2> a3 = { {std::string("a"), "b"} }; |
Revision as of 13:01, 16 April 2013
Defined in header <array>
|
||
template< class T, |
(since C++11) | |
std::array
is a container that encapsulates constant size arrays.
This struct has the same aggregate type semantics as a C-style array. The size and efficiency of array<T,N> for some number of elements is equivalent to size and efficiency of the corresponding C-style array T[N]
. The struct provides 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.
array
is an aggregate (it has no user-defined constructors and no private or protected members), which allows it to use aggregate-initialization.
An array can also be used as a tuple of N
elements of the same type.
Contents |
Member types
Member functions
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
Helper classes
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