std::swap(std::vector)
cppreference.net에서
| (C++17까지) <vector>
|
||
template< class T, class Alloc >
void swap( std::vector<T, Alloc>& lhs,
std::vector<T, Alloc>& rhs );
|
헤더에 정의됨 | |
template< class T, class Alloc >
void swap( std::vector<T, Alloc>& lhs,
std::vector<T, Alloc>& rhs )
noexcept(/* see below */);
|
(C++17부터) (C++20부터 constexpr) |
|
std::swap 알고리즘을 std::vector에 대해 특수화합니다. lhslhs와 rhsrhslhs.swap(rhs)의 내용을 교환합니다.
매개변수
| lhs, rhs | - | 내용을 교환할 컨테이너 |
복잡도
상수 시간.
예외
|
noexcept 명시:
noexcept(noexcept(lhs.swap(rhs)))
|
(C++17부터) |
예제
이 코드 실행
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> alice{1, 2, 3};
std::vector<int> bob{7, 8, 9, 10};
auto print = [](const int& n) { std::cout << ' ' << n; };
// Print state before swap
std::cout << "Alice:";
std::for_each(alice.begin(), alice.end(), print);
std::cout << "\nBobby:";
std::for_each(bob.begin(), bob.end(), print);
std::cout << '\n';
std::cout << "-- SWAP\n";
std::swap(alice, bob);
// Print state after swap
std::cout << "Alice:";
std::for_each(alice.begin(), alice.end(), print);
std::cout << "\nBobby:";
std::for_each(bob.begin(), bob.end(), print);
std::cout << '\n';
}
출력:
Alice: 1 2 3
Bobby: 7 8 9 10
-- SWAP
Alice: 7 8 9 10
Bobby: 1 2 3
참조 항목
| 내용을 교환합니다 (public 멤버 함수) |