Difference between revisions of "cpp/container/array"
(Works without double braces in gcc and clang. Clang produces warnings for both a1 {1, 2, 3}; and a2 = {1,2,3};. So one should either always use double braces or never use them.) |
(Undo revision 74878 by 89.254.222.207 (talk) and add comments to explain) |
||
Line 96: | Line 96: | ||
{ | { | ||
// 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 in C++11 (not in C++14) |
− | std::array<int, 3> a2 = {1, 2, 3}; | + | std::array<int, 3> a2 = {1, 2, 3}; // never required after = |
std::array<std::string, 2> a3 = { std::string("a"), "b" }; | std::array<std::string, 2> a3 = { std::string("a"), "b" }; | ||
Revision as of 06:49, 29 December 2014
Defined in header <array>
|
||
template< class T, |
(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. 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 |
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
| ||||||
size_type
|
std::size_t | ||||||
difference_type
|
std::ptrdiff_t | ||||||
reference
|
value_type& | ||||||
const_reference
|
const value_type& | ||||||
pointer
|
value_type* | ||||||
const_pointer
|
const value_type* | ||||||
iterator
|
| ||||||
const_iterator
|
| ||||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
Member functions
Implicitly-defined member functions | |
(constructor) (implicitly declared) |
default-initializes or copy-initializes 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 | |
access specified element with bounds checking (public member function) | |
access specified element (public member function) | |
access the first element (public member function) | |
access the last element (public member function) | |
direct access to the underlying contiguous storage (public member function) | |
Iterators | |
returns an iterator to the beginning (public member function) | |
returns an iterator to the end (public member function) | |
returns a reverse iterator to the beginning (public member function) | |
returns a reverse iterator to the end (public member function) | |
Capacity | |
checks whether the container is empty (public member function) | |
returns the number of elements (public member function) | |
returns the maximum possible number of elements (public member function) | |
Operations | |
fill the container with specified value (public member function) | |
swaps the contents (public member function) |
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 array s (function template) |
(C++11) |
accesses an element of an array (function template) |
(C++11) |
specializes the std::swap algorithm (function template) |
Helper classes
(C++11) |
obtains the size of an array (class template specialization) |
obtains the type of the elements of array (class template specialization) |
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 (not in C++14) std::array<int, 3> a2 = {1, 2, 3}; // 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 << ' '; }
Output:
3 2 1 a b