set swap() in C++

swap(): This is a build-in function in STL. This function swaps values of two sets.

Parameters: One parameter is required for this function.

__l: A set of elements of the same type value.

Syntax:

st.swap(__l)

For Example:

st = {1,2,3,4}

st1= {5,6,7,10,11}

st.swap(st1) = > st = {5,6,7,10,11}, st1 = {1,2,3,4}

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    set<intst;
    st.insert({1243});

    set<intst1;
    st1.insert({5671011});
    st.swap(st1);

    for (auto it = st.begin(); it != st.end(); it++)
        cout << *it << " ";
    cout << "\n";

    for (auto it = st1.begin(); it != st1.end(); it++)
        cout << *it << " ";
   
    return 0;
}


No comments:

Post a Comment