Namespaces
Variants

std:: swap (std::queue)

From cppreference.net

헤더 파일에 정의됨 <queue>
template < class T, class Container >

void swap ( std:: queue < T, Container > & lhs,

std:: queue < T, Container > & rhs ) ;
(C++11부터)
(C++17까지)
template < class T, class Container >

void swap ( std:: queue < T, Container > & lhs,
std:: queue < T, Container > & rhs )

noexcept ( /* see below */ ) ;
(C++17부터)
(C++26부터 constexpr)
Specializes the std::swap algorithm for std::queue . Swaps the contents of lhs and rhs . Calls lhs. swap ( rhs ) .

이 오버로드는 다음 조건이 std:: is_swappable_v < Container > 일 때에만 오버로드 해결에 참여합니다.

(since C++17)

목차

매개변수

lhs, rhs - 내용을 교환할 컨테이너들

복잡도

기본 컨테이너를 교환하는 것과 동일합니다.

예외

noexcept 명세:
noexcept ( noexcept ( lhs. swap ( rhs ) ) )
(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

참고 항목

(C++11)
내용을 교환합니다
(public member function)