swap(): This function is available in the File: unordered_set.h. This function is a build-in function in STL of C++. This function swaps data with another unordered_set.
Syntax:
void std::unordered_set<int>::swap(std::unordered_set<int> &__x)
This function takes one argument of type unordered_set as its parameter. This function swaps data with another unordered_set.
Parameters: One parameter is required for this method.
__x – An unordered_set of the same element and allocator types.
This exchanges the elements between two sets in constant time.
File: unordered_set.h
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){unordered_set<int> st1 = {9, 4, 5};unordered_set<int> st2 = {10, 25, 124, 67};cout << "unordered_set 1 size before swap :" <<st1.size() << "\n";cout << "unordered_set 2 size before swap :" <<st2.size() << "\n";st1.swap(st2);cout << "\n";cout << "unordered_set 1 size after swap :" <<st1.size() << "\n";cout << "unordered_set 2 size after swap :" <<st2.size() << "\n";return 0;}
Output:
unordered_set 1 size before swap :3 unordered_set 2 size before swap :4 unordered_set 1 size after swap :4 unordered_set 2 size after swap :3
No comments:
Post a Comment