Namespaces
Variants

std::multiset<Key,Compare,Allocator>:: swap

From cppreference.net

void swap ( multiset & other ) ;
(C++17까지)
void swap ( multiset & other ) noexcept ( /* see below */ ) ;
(C++17부터)
(C++26부터 constexpr)

컨테이너의 내용을 other 와 교환합니다. 개별 요소에 대한 이동, 복사, 교환 연산을 호출하지 않습니다.

모든 반복자와 참조는 유효하게 유지됩니다. end() 반복자는 무효화됩니다. Compare Swappable 해야 하며, 이 타입의 객체들은 비멤버 swap 에 대한 한정되지 않은 호출을 사용하여 교환됩니다.

만약 std:: allocator_traits < allocator_type > :: propagate_on_container_swap :: value true 라면, 할당자들은 비멤버 swap 에 대한 한정되지 않은 호출을 사용하여 교환됩니다. 그렇지 않으면, 그들은 교환되지 않습니다 (그리고 만약 get_allocator ( ) ! = other. get_allocator ( ) 라면, 동작은 정의되지 않습니다).

(C++11부터)

목차

매개변수

other - 내용을 교환할 대상 컨테이너

예외

Compare 객체들의 swap에 의해 발생하는 모든 예외.

(until C++17)
noexcept 명세:
noexcept ( std:: allocator_traits < Allocator > :: is_always_equal :: value
&& std:: is_nothrow_swappable < Compare > :: value )
(since C++17)

복잡도

상수.

예제

#include <functional>
#include <iostream>
#include <set>
template<class Os, class Co>
Os& operator<<(Os& os, const Co& co)
{
    os << '{';
    for (const auto& i : co)
        os << ' ' << i;
    return os << " } ";
}
int main()
{
    std::multiset<int> a1{3, 1, 3, 2}, a2{5, 4, 5};
    auto it1 = std::next(a1.begin());
    auto it2 = std::next(a2.begin());
    const int& ref1 = *(a1.begin());
    const int& ref2 = *(a2.begin());
    std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n';
    a1.swap(a2);
    std::cout << a1 << a2 << *it1 << ' ' << *it2 << ' ' << ref1 << ' ' << ref2 << '\n';
    // 스왑 전 한 컨테이너의 요소를 참조하는 모든 반복자는
    // 스왑 후 다른 컨테이너의 동일한 요소를 참조합니다. 참조에 대해서도 동일하게 적용됩니다.
    struct Cmp : std::less<int>
    {
        int id{};
        Cmp(int i) : id{i} {}
    };
    std::multiset<int, Cmp> s1{{2, 2, 1, 1}, Cmp{6}}, s2{{4, 4, 3, 3}, Cmp{9}};
    std::cout << s1 << s2 << s1.key_comp().id << ' ' << s2.key_comp().id << '\n';
    s1.swap(s2);
    std::cout << s1 << s2 << s1.key_comp().id << ' ' << s2.key_comp().id << '\n';
    // 따라서 비교자 객체(Cmp)도 스왑 후 교환됩니다.
}

출력:

{ 1 2 3 3 } { 4 5 5 } 2 5 1 4
{ 4 5 5 } { 1 2 3 3 } 2 5 1 4
{ 1 1 2 2 } { 3 3 4 4 } 6 9
{ 3 3 4 4 } { 1 1 2 2 } 9 6

참고 항목

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