이름공간
변수
행위

std::array

cppreference.com
< cpp‎ | container
<array> 에 정의되어 있음.
template<

    class T,
    std::size_t N

> struct array;
(since C++11)

std::array is a container that encapsulates constant 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.

목차

[편집] 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 RandomAccessIterator [edit]
const_iterator 상수(constant) 양방향 반복자(iterator) [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)
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
access specified element with bounds checking
(public member function) [edit]
특정 원소에 접근한다
(public member function) [edit]
첫번째 요소에 접근한다.
(public member function) [edit]
마지막 요소에 접근한다.
(public member function) [edit]
direct access to the underlying array
(public member function) [edit]
Iterators
첫번째 원소로의 반복자(iterator)를 반환한다.
(public member function) [edit]
마지막 원소로의 반복자(iterator)를 반환한다.
(public member function) [edit]
첫번째 원소로의 역방향 반복자(reverse iterator)를 반환한다.
(public member function) [edit]
마지막 원소로의 역방향 반복자(reverse iterator)를 반환한다.
(public member function) [edit]
Capacity
현재 컨테이너가 비어있는지 확인한다.
(public member function) [edit]
원소의 개수를 반환한다.
(public member function) [edit]
원소의 최대 개수를 반환한다.
(public member function) [edit]
Operations
fill the container with specified value
(public member function) [edit]
원소들을 서로 바꾼다
(public member function) [edit]

[편집] Non-member functions

lexicographically compares the values in the array
(function template) [edit]
accesses an element of an array
(function template) [edit]
specializes the std::swap algorithm
(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]

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