std::swap

std::swap (std::priority_queue 특수화)

std::swap 알고리즘을 std::priority_queue에 대해 특수화한 함수예요. 두 priority_queue의 내용물을 서로 바꿔요. 내부적으로 lhs.swap(rhs)를 호출해요.

출처: cppreference

본문

<queue> 헤더에 정의돼 있고, std::priority_queueswap 오버로드예요.

template< class T, class Container, class Compare >
void swap( std::priority_queue<T, Container, Compare>& lhs,
           std::priority_queue<T, Container, Compare>& rhs );  // (until C++17)

template< class T, class Container, class Compare >
void swap( std::priority_queue<T, Container, Compare>& lhs,
           std::priority_queue<T, Container, Compare>& rhs )
               noexcept(/* see below */);                     // (since C++17)

lhsrhs의 내용물을 서로 바꿔요. 내부적으로 lhs.swap(rhs)를 호출해요. C++17부터 이 오버로드는 std::is_swappable_v<Container>std::is_swappable_v<Compare>가 모두 true일 때만 오버로드 해석에 참여해요.

  • 매개변수 lhs, rhs: 내용물을 바꿀 컨테이너 어댑터들.
  • 복잡도: 기본 컨테이너를 swap하는 것과 같아요.
  • 예외 지정: noexcept(noexcept(lhs.swap(rhs))) (C++17부터).

참고로 컨테이너 어댑터용 std::swap 오버로드가 C++11에 도입됐지만, C++98에서도 std::swap으로 어댑터를 이미 swap할 수 있었어요. 이 특수화는 어댑터 내부 컨테이너·비교기에 접근할 수 있으므로 더 효율적인 swap이 가능해요.

더 알아보기 (Learn more)

cppreference