#!if 문서명2 != null
, [[라이브러리]]
#!if 문서명3 != null
, [[배열]]
#!if 문서명4 != null
, [[]]
#!if 문서명5 != null
, [[]]
#!if 문서명6 != null
, [[]]
1. 개요
||<-12><tablewidth=80%><tablebordercolor=#20b580><tablebgcolor=#090f0a,#050b09><tablecolor=#a8a8a8,#c1c1c1><rowcolor=#d7d7d7,#a1a1a1>
<vector>
||
| |||||||||||
<bgcolor=#20b580> | |||||||||||
<colcolor=#f0f0f8,#e1e1e1>C++11에서 추가된 모듈 는 클래스 std::array<T, Length> 및 관련 유틸리티를 제공한다. | |||||||||||
<bgcolor=#20b580> |
<array>
배열 클래스
`std::array<T, Length>`
[1]는 자료형 `T`
를 담은 길이 `Length`
의 정적 배열을 추상화한 클래스다. 템플릿 상수 매개변수를 통해 컴파일 시점에 배열의 크기가 정해지며 바꿀 수 없다. 정적 배열의 얕은 래퍼일 뿐이라서 성능에 불이익은 없으며 멤버 함수를 비롯하여 편리한 유틸리티를 이용할 수 있다. C++의 표준 이용에 필요한 멤버도 정의되어 있으므로 이 클래스를 사용하는 것이 좋다.2. 생성자
#!syntax cpp
T array[Length];
어래이 클래스는 상기 코드의 정적 배열을 잘 쓰기 위한 유틸리티의 집합이다. 별도의 생성자가 존재하지 않는다. 대신 집결 초기화를 사용할 수 있는 클래스다. 이 클래스는 데이터 멤버로 C언어의 배열을 갖고 있어서 생성자에 원소를 여러개 전달하면 배열에 저장된다. 기존 배열과 호환성을 유지하고 라이브러리 유지관리 부하를 줄이기 위해 이렇게 제정되었다.#!syntax cpp
std::array<T, Length> list();
std::array<T, Length> list{};
// C
T array[Length]{};
기본 초기화를 쓰면 내부적으로 저장된 배열을 초기화한다. 이 경우 원소들의 기본 생성자를 호출한다.#!syntax cpp
std::array<int, 6> list({ 0, 1, 2, 3, 4, 5 });
std::array<int, 6> list{ 0, 1, 2, 3, 4, 5 };
// C
int array[6]{ 0, 1, 2, 3, 4, 5 };
// C++17
// `list`는 자동으로 std::array<int, 6>으로 추론됨.
std::array list{ 0, 1, 2, 3, 4, 5 };
initializer_list<T>
로 초기화하면 배열의 성분을 초기화할 수 있다. C++17 부터는 연역 유도문을 지원하여 암시적으로 인스턴스를 생성할 수 있다.3. 원소 참조
#!syntax cpp
std::array<int, 10> list{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// `v0`은 6
int& v0 = list[5];
// `v1`은 10
const int& v1 = list[9];
// `v2`은 5
// `list` = { 1, 2, 3, 4, -1, 6, 7, 8, 9, 10 }
int v2 = std::move(std::exchange(list[4], -1));
// `v3`은 3
int& v3 = list.at(2);
하위 참조 연산자 [size_t index]
와 멤버 함수 at(size_t index)
& at(size_t index) const
를 써서 원소를 가져올 수 있다.#!syntax cpp
std::array list{ 1, 2, 3, 4, 5 };
// `list` = { 1, 2, 3, 4, 10 }
list[4] = 10;
// `list` = { 1, 2, 3, 50, 10 }
list.at(3) = 50;
auto it = list.begin() + 2;
// `list` = { 1, 2, 80, 50, 10 }
*it = 80;
마찬가지로 원소 수정도 할 수 있다.4. 순회자
#!syntax cpp
std::array<int, 10> list{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::print("배열 원소 ({}개): ", list.size());
// `i`는 `int&`
for (auto& i : list)
{
std::print("{} ", i);
}
// '배열 원소 (10개): 1 2 3 4 5 6 7 8 9 10' 출력
// `list` = { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 }
std::transform(list.begin(), list.end(), [](const int& v) { return v * 3; });
순회자와 범위 기반 반복문을 사용할 수 있다.5. 복사와 이동
#!syntax cpp
// int는 복사할 수 있는 자명한 자료형
std::array<int, 5> list1{ 0, 1, 2, 3, 4 };
// std::array<int, 5>
std::array list2{ 5, 6, 7, 8, 9 };
// 복사 대입
list1 = list2;
// 복사 생성
auto list3 = list1;
어래이 클래스는 저장된 자료형 `T`
에 복사와 이동 여부가 결정된다. 가령 int
따위의 자명한 자료형은 복사와 이동이 모두 가능하다. std::string
같은 비자명한 자료형이라도 복사와 이동 생성자/대입 연산자가 구현되어 있으면 std::array<std::string, Length>
로 복사와 이동이 가능하다.하지만 자료형과 크기가 모두 같은 인스턴스 끼리만 복사와 이동을 할 수 있음을 알아야 한다. 그 경우
std::copy
내지는 std::transform
등의 별도의 변환 함수를 사용해야 한다.==# 코드 요약 #==
#!syntax cpp
#include <compare>
#include <initializer_list>
namespace std
{
template<class T, size_t N>
struct array;
template<class T, size_t N>
constexpr bool operator==(const array<T, N>& x, const array<T, N>& y);
template<class T, size_t N>
constexpr /*synth-three-way-result*/<T> operator<=>(const array<T, N>& x,
const array<T, N>& y);
// specialized algorithms
template<class T, size_t N>
constexpr void swap(array<T, N>& x, array<T, N>& y) noexcept(noexcept(x.swap(y)));
// array creation functions
template<class T, size_t N>
constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]);
template<class T, size_t N>
constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]);
// tuple interface
template<class T>
struct tuple_size;
template<size_t I, class T>
struct tuple_element;
template<class T, size_t N>
struct tuple_size<array<T, N>>;
template<size_t I, class T, size_t N>
struct tuple_element<I, array<T, N>>;
template<size_t I, class T, size_t N>
constexpr T& get(array<T, N>&) noexcept;
template<size_t I, class T, size_t N>
constexpr T&& get(array<T, N>&&) noexcept;
template<size_t I, class T, size_t N>
constexpr const T& get(const array<T, N>&) noexcept;
template<size_t I, class T, size_t N>
constexpr const T&& get(const array<T, N>&&) noexcept;
}
template<class T, size_t N>
struct array
{
// types
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = size_t;
using difference_type = ptrdiff_t;
using iterator = /* implementation-defined */;
using const_iterator = /* implementation-defined */;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
// no explicit construct/copy/destroy for aggregate type
constexpr void fill(const T& u);
constexpr void swap(array&) noexcept(is_nothrow_swappable_v<T>);
// iterators
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
constexpr reverse_iterator rbegin() noexcept;
constexpr const_reverse_iterator rbegin() const noexcept;
constexpr reverse_iterator rend() noexcept;
constexpr const_reverse_iterator rend() const noexcept;
constexpr const_iterator cbegin() const noexcept;
constexpr const_iterator cend() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;
// capacity
constexpr bool empty() const noexcept;
constexpr size_type size() const noexcept;
constexpr size_type max_size() const noexcept;
// element access
constexpr reference operator[](size_type n);
constexpr const_reference operator[](size_type n) const;
constexpr reference at(size_type n); // freestanding-deleted
constexpr const_reference at(size_type n) const; // freestanding-deleted
constexpr reference front();
constexpr const_reference front() const;
constexpr reference back();
constexpr const_reference back() const;
constexpr T* data() noexcept;
constexpr const T* data() const noexcept;
};
template<class T, class... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>;
6. 둘러보기
||<:><-12><width=90%><tablewidth=80%><tablebordercolor=#20b580><rowbgcolor=#090f0a,#050b09><rowcolor=#d7d7d7,#a1a1a1>C++||||
}}}
}}}||
}}}||
C언어와의 차이점 | 학습 자료 | 평가 | |||||||||||||
<bgcolor=#20b580> | |||||||||||||||
<rowcolor=#090912,#bebebf>C++ 문법 | |||||||||||||||
<bgcolor=#ffffff> | |||||||||||||||
main | 헤더 | 모듈 | |||||||||||||
함수 | 구조체 | 이름공간 | |||||||||||||
한정자 | 참조자 | 포인터 | |||||||||||||
클래스 | 값 범주론 | 특성 | |||||||||||||
auto | using | decltype | |||||||||||||
상수 표현식 | 람다 표현식 | 객체 이름 검색 | |||||||||||||
템플릿 | 템플릿 제약조건 | 메타 프로그래밍 | |||||||||||||
<bgcolor=#20b580> | |||||||||||||||
<rowcolor=#090912,#bebebf>C++ 버전 | |||||||||||||||
<bgcolor=#ffffff> | |||||||||||||||
C++26 | C++23 | C++20 | |||||||||||||
C++17 | C++14 | C++11 | |||||||||||||
C++03 | C++98 | C with Classes | |||||||||||||
<bgcolor=#20b580> | |||||||||||||||
<rowcolor=#090912,#bebebf>C++ 표준 라이브러리 | |||||||||||||||
<rowcolor=#090912,#bebebf>문서가 있는 모듈 목록 | |||||||||||||||
<bgcolor=#ffffff> | |||||||||||||||
#개요 | C++11 #개요 | <unordered_map> C++11 #개요 | |||||||||||||
C++20 #개요 | #개요 | #개요 | |||||||||||||
C++11 #개요 | C++11 #개요 | C++17 #개요 | |||||||||||||
#개요 | <string_view> C++17 #개요 | C++20 #개요 | |||||||||||||
C++11 #개요 | C++11 #개요 | C++11 #개요 | |||||||||||||
C++20 #개요 | C++23 #개요 | ||||||||||||||
<bgcolor=#20b580> | |||||||||||||||
<rowcolor=#090912,#bebebf>예제 목록 | |||||||||||||||
<bgcolor=#ffffff> | |||||||||||||||
{{{#!wiki style=""text-align: center, margin: 0 -10px" {{{#!folding [ 펼치기 · 접기 ] | 동기화 전략 1 임계 영역과 상호 배제 | 동기화 전략 2 원자적 연산과 메모리 장벽 | 동시성 자료 구조 1 큐 구현 | 동시성 자료 구조 2 집합 구현 | |||||||||||
함수 템플릿 일반화 프로그래밍 | 전이 참조 완벽한 매개변수 전달 | 튜플 구현 가변 클래스 템플릿 | 직렬화 함수 구현 템플릿 매개변수 묶음 | ||||||||||||
SFINAE 1 멤버 함수 검사 | SFINAE 2 자료형 태그 검사 | SFINAE 3 메타 데이터 | SFINAE 4 자료형 트레잇 | ||||||||||||
제약조건 1 개념 (Concept) | 제약조건 2 상속 여부 검사 | 제약조건 3 클래스 명세 파헤치기 | 제약조건 4 튜플로 함자 실행하기 | ||||||||||||
메타 프로그래밍 1 특수화 여부 검사 | 메타 프로그래밍 2 컴파일 시점 문자열 | 메타 프로그래밍 3 자료형 리스트 | 메타 프로그래밍 4 안전한 union |
}}}||
<bgcolor=#20b580> | |||||||||||||||
<rowcolor=#090912,#bebebf>외부 링크 | |||||||||||||||
<bgcolor=#ffffff> | |||||||||||||||
{{{#!wiki style=""text-align: center, margin: 0 -10px" | |||||||||||||||
<bgcolor=#20b580> | |||||||||||
<rowcolor=#090912,#bebebf>C++ |
[1] 이하 `어래이 클래스`