std::array<T,N>:: operator[]
|
reference operator
[
]
(
size_type pos
)
;
|
(1) |
(C++11부터)
(C++17부터 constexpr) |
|
const_reference operator
[
]
(
size_type pos
)
const
;
|
(2) |
(C++11부터)
(C++14부터 constexpr) |
지정된 위치 pos 의 요소에 대한 참조를 반환합니다.
|
pos < size ( ) 가 false 인 경우, 동작은 정의되지 않습니다. |
(C++26 이전) |
|
pos < size ( ) 가 false 인 경우: |
(C++26 이후) |
목차 |
매개변수
| pos | - | 반환할 요소의 위치 |
반환값
요청된 요소에 대한 참조입니다.
복잡도
상수.
참고 사항
std::map::operator[] 와 달리, 이 연산자는 컨테이너에 새로운 요소를 절대 삽입하지 않습니다. 이 연산자를 통해 존재하지 않는 요소에 접근하는 것은 정의되지 않은 동작입니다 , 구현이 강화된 경우는 제외 (C++26부터) .
예제
다음 코드는 operator [ ] 를 사용하여 std:: array < int , N > 에서 읽고 쓰는 방법을 보여줍니다:
#include <array> #include <iostream> int main() { std::array<int, 4> numbers{2, 4, 6, 8}; std::cout << "Second element: " << numbers[1] << '\n'; numbers[0] = 5; std::cout << "All numbers:"; for (auto i : numbers) std::cout << ' ' << i; std::cout << '\n'; }
출력:
Second element: 4 All numbers: 5 4 6 8
참고 항목
|
경계 검사와 함께 지정된 요소에 접근
(public member function) |