std:: swap (std::queue)
|
헤더 파일에 정의됨
<queue>
|
||
|
template
<
class
T,
class
Container
>
void
swap
(
std::
queue
<
T, Container
>
&
lhs,
|
(C++11부터)
(C++17까지) |
|
|
template
<
class
T,
class
Container
>
void
swap
(
std::
queue
<
T, Container
>
&
lhs,
|
(C++17부터)
(C++26부터 constexpr) |
|
|
이 오버로드는 다음 조건이 std:: is_swappable_v < Container > 일 때에만 오버로드 해결에 참여합니다. |
(since C++17) |
목차매개변수
복잡도기본 컨테이너를 교환하는 것과 동일합니다. 예외
참고 사항컨테이너 어댑터에 대한 std::swap 오버로드가 C++11에서 도입되었지만, 컨테이너 어댑터는 C++98에서 이미 std::swap 으로 교환될 수 있었습니다. 이러한 std::swap 호출은 일반적으로 선형 시간 복잡도를 가지지만, 더 나은 복잡도가 제공될 수 있습니다. 예제
이 코드 실행
#include <algorithm> #include <iostream> #include <queue> int main() { std::queue<int> alice; std::queue<int> bob; auto print = [](const auto& title, const auto& cont) { std::cout << title << " size=" << cont.size(); std::cout << " front=" << cont.front(); std::cout << " back=" << cont.back() << '\n'; }; for (int i = 1; i < 4; ++i) alice.push(i); for (int i = 7; i < 11; ++i) bob.push(i); // 스왑 전 상태 출력 print("Alice:", alice); print("Bobby:", bob); std::cout << "-- SWAP\n"; std::swap(alice, bob); // 스왑 후 상태 출력 print("Alice:", alice); print("Bobby:", bob); } 출력: Alice: size=3 front=1 back=3 Bobby: size=4 front=7 back=10 -- SWAP Alice: size=4 front=7 back=10 Bobby: size=3 front=1 back=3 참고 항목
|