Namespaces
Variants

std::priority_queue<T,Container,Compare>:: swap

From cppreference.net

void swap ( priority_queue & other ) noexcept ( /* see below */ ) ;
(C++11 이후)
Exchanges the contents of the container adaptor with those of other . Effectively calls
using std::swap;
swap(c, other.c);
swap(comp, other.comp);

목차

매개변수

other - 내용을 교환할 컨테이너 어댑터

반환값

(없음)

예외

noexcept 명세:
noexcept ( noexcept ( swap ( c, other. c ) ) && noexcept ( swap ( comp, other. comp ) ) )

위 표현식에서 식별자 swap 은 C++17 std::is_nothrow_swappable 특성(trait)이 사용하는 방식과 동일한 방식으로 조회됩니다.

(C++11부터)
(C++17까지)
noexcept 명세:
noexcept ( std:: is_nothrow_swappable_v < Container > &&
std:: is_nothrow_swappable_v < Compare > )
(C++17부터)

복잡도

기반 컨테이너와 동일합니다 (일반적으로 상수).

참고 사항

일부 구현(예: libc++)에서는 C++11 이전 모드에 대한 확장으로 swap 멤버 함수를 제공합니다.

예제

#include <iostream>
#include <concepts>
#include <functional>
#include <queue>
#include <string>
#include <string_view>
#include <vector>
template<typename Adaptor>
requires (std::ranges::input_range<typename Adaptor::container_type>)
void print(std::string_view name, const Adaptor& adaptor)
{
    struct Printer : Adaptor // to use protected Adaptor::Container c;
    {
        void print(std::string_view name) const
        {
            std::cout << name << " [" << std::size(this->c) << "]: ";
            for (auto const& elem : this->c)
                std::cout << elem << ' ';
            std::cout << '\n';
        }
    };
    static_cast<Printer const&>(adaptor).print(name);
}
int main()
{
    std::vector<std::string> v1{"1","2","3","4"},
                             v2{"Ɐ","B","Ɔ","D","Ǝ"};
    std::priority_queue s1(std::less<>(), std::move(v1));
    std::priority_queue s2(std::less<>(), std::move(v2));
    print("s1", s1);
    print("s2", s2);
    s1.swap(s2);
    print("s1", s1);
    print("s2", s2);
}

출력:

s1 [4]: 4 3 2 1
s2 [5]: Ǝ D Ɔ B Ɐ
s1 [5]: Ǝ D Ɔ B Ɐ
s2 [4]: 4 3 2 1

결함 보고서

다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2456 C++11 noexcept 명세가 올바르지 않은 형태임 작동하도록 수정됨

참고 항목

std::swap 알고리즘을 특수화함
(함수 템플릿)