std::inplace_vector<T,N>:: swap
From cppreference.net
<
cpp
|
container
|
inplace vector
|
constexpr
void
swap
(
inplace_vector
&
other
)
noexcept
(
/* see below */
)
;
|
(C++26부터) | |
컨테이너의 내용을 other 와 교환합니다. 반복자와 참조가 다른 컨테이너와 연관되지 않도록 합니다.
목차 |
매개변수
| other | - | 내용을 교환할 대상 컨테이너 |
반환값
(없음)
예외
noexcept
명세:
noexcept
(
N
==
0
||
( std:: is_nothrow_swappable_v < T > && std:: is_nothrow_move_constructible_v < T > ) )
( std:: is_nothrow_swappable_v < T > && std:: is_nothrow_move_constructible_v < T > ) )
복잡도
컨테이너 크기에 선형적으로 비례합니다.
예제
이 코드 실행
#include <inplace_vector> #include <print> int main() { std::inplace_vector<int, 3> a1{1, 2, 3}, a2{4, 5, 6}; auto i1 = a1.begin(); auto i2 = a2.begin(); int& r1 = a1[1]; int& r2 = a2[1]; auto print_them_all = [&](auto rem) { std::println("{}a1 = {}, a2 = {}, *i1 = {}, *i2 = {}, r1 = {}, r2 = {}", rem, a1, a2, *i1, *i2, r1, r2); }; print_them_all("Before swap:\n"); a1.swap(a2); print_them_all("After swap:\n"); // swap() 이후 반복자와 참조는 원래 연결된 요소와의 연관성을 유지합니다. // 예: i1은 a1[0] 요소를 가리키고, r1은 a1[1]을 참조합니다. }
출력:
Before swap: a1 = [1, 2, 3], a2 = [4, 5, 6], *i1 = 1, *i2 = 4, r1 = 2, r2 = 5 After swap: a1 = [4, 5, 6], a2 = [1, 2, 3], *i1 = 4, *i2 = 1, r1 = 5, r2 = 2
참고 항목
|
(C++26)
|
std::swap
알고리즘을 특수화함
(함수 템플릿) |