set-swap

////////////////////////////////////////
//      2018/04/29 15:25:15
//      set-swap

// exchange two sets
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>

using namespace std;

void print(set<int, less<int>>& s){
    copy(s.begin(), s.end(), ostream_iterator<int>(cout," "));
    cout << endl;
}
//-------------------

int main(){
    int ary1[] = { 1, 2, 3, 2, 3, 4, 8, 2, 5, 6 };
    int ary2[] = { 5, 0, 9, 2, 3, 4, 8, 2, 5, 6 };


    set<int, less<int>> s1, s2;

    s1.insert(ary1, ary1 + 10);
    cout << "s1:";
    print(s1);

    s2.insert(ary2,ary2+10);
    cout << "s2:";
    print(s2);

    if (s1 != s2){
        s1.swap(s2);
    }else{
        cout << "They are the same!" << endl;
    }

    cout << "s1:";
    print(s1);

    cout << "s2:";
    print(s2);

    return 0;
}

/*
OUTPUT:
    s1:1 2 3 4 5 6 8
    s2:0 2 3 4 5 6 8 9
    s1:0 2 3 4 5 6 8 9
    s2:1 2 3 4 5 6 8
*/ 
原文地址:https://www.cnblogs.com/laohaozi/p/12537890.html