std::deque<T,Allocator>:: operator[]
|
reference operator
[
]
(
size_type pos
)
;
|
(1) | (constexpr since C++26) |
|
const_reference operator
[
]
(
size_type pos
)
const
;
|
(2) | (constexpr since C++26) |
지정된 위치 pos 의 요소에 대한 참조를 반환합니다.
|
pos < size ( ) 가 false 인 경우, 동작은 정의되지 않습니다. |
(C++26 이전) |
|
pos < size ( ) 가 false 인 경우: |
(C++26 이후) |
목차 |
매개변수
| pos | - | 반환할 요소의 위치 |
반환값
요청된 요소에 대한 참조입니다.
복잡도
상수.
참고 사항
std::map::operator[] 와 달리, 이 연산자는 컨테이너에 새로운 요소를 절대 삽입하지 않습니다. 이 연산자를 통해 존재하지 않는 요소에 접근하는 것은 정의되지 않은 동작입니다 , 구현이 강화된 경우를 제외하고 (C++26부터) .
예제
다음 코드는 operator [ ] 를 사용하여 std:: deque < int > 에 읽기 및 쓰기를 수행합니다:
#include <deque> #include <iostream> int main() { std::deque<int> 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) |