
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_set::swap() function
The C++ std::unordered_set::swap() function is used to exchange the contents of the unordered_set with others. If both the unordered_sets have the same contents, it does nothing; otherwise, exchange the contents of the unordered_sets with another unordered_set. The return type of this function is void which implies that it does not return any value.
Syntax
Following is the syntax of std::unordered_set::swap() function −
void swap ( unordered_set& other);
Parameters
- other − It is another container to exchange the contents with.
Return Value
This function does not return any value.
Example 1
Let's look at the following example, where we are going to exchange the contents of the num_set1 with the contents of the num_set2.
#include<iostream> #include<string> #include<unordered_set> using namespace std; int main () { //create unordered_sets unordered_set<int> num_set1 = {1, 2, 3, 4, 5}; unordered_set<int> num_set2 = {6, 7, 8, 9, 10}; cout<<"Contents of the num_set1 before swap operation: "<<endl; for(int n1 : num_set1){ cout<<n1<<endl; } cout<<"Contents of the num_set2 before swap operation: "<<endl; for(int n2 : num_set2){ cout<<n2<<endl; } //using the swap() function num_set1.swap(num_set2); cout<<"Contents of the num_set1 after swap operation: "<<endl; for(int n1 : num_set1){ cout<<n1<<endl; } cout<<"Contents of the num_set2 after swap operation: "<<endl; for(int n2 : num_set2){ cout<<n2<<endl; } }
Output
If we run the above code it will generate the following output −
Contents of the num_set1 before swap operation: 5 4 3 2 1 Contents of the num_set2 before swap operation: 10 9 8 7 6 Contents of the num_set1 after swap operation: 10 9 8 7 6 Contents of the num_set2 after swap operation: 5 4 3 2 1
Example 2
Consider the another scenario, where we are going to consider the unordered_sets of char type and applying the swap() function.
#include<iostream> #include<string> #include<unordered_set> using namespace std; int main () { //create unordered_sets unordered_set<char> char_set1 = {}; unordered_set<char> char_set2 = {'a', 'b', 'c', 'd'}; cout<<"Size of the char_set1 before swap operation: "<<char_set1.size()<<endl; cout<<"Contents of the char_set2 before swap operation: "<<endl; for(char c2 : char_set2){ cout<<c2<<endl; } //using the swap() function char_set1.swap(char_set2); cout<<"Contents of the char_set1 after swap operation: "<<endl; for(char c1 : char_set1){ cout<<c1<<endl; } cout<<"Size of the char_set2 after the swap operation: "<<char_set2.size(); }
Output
Following is the output of the above code −
Size of the char_set1 before swap operation: 0 Contents of the char_set2 before swap operation: d c b a Contents of the char_set1 after swap operation: d c b a Size of the char_set2 after the swap operation: 0
Example 3
In the following example, we are going to consider the unordered_set of string type and applying the swap() function to exchange the contents of the unordered_set.
#include<iostream> #include<string> #include<unordered_set> using namespace std; int main () { //create unordered_sets unordered_set<string> str_set1 = {"Red", "Blue", "Yellow", "White"}; unordered_set<string> str_set2 = {"Apple", "Banana", "Mango", "Orange"}; cout<<"Contents of the str_set1 before swap operation: "<<endl; for(string s1 : str_set1){ cout<<s1<<endl; } cout<<"Content of the str_set2 before swap operation: "<<endl; for(string s2 : str_set2){ cout<<s2<<endl; } //using swap() operation str_set1.swap(str_set2); cout<<"Contents of the str_set1 after swap operation: "<<endl; for(string s1 : str_set1){ cout<<s1<<endl; } cout<<"Content of the str_set2 after swap operation: "<<endl; for(string s2 : str_set2){ cout<<s2<<endl; } }
Output
Output of the above code is as follows −
Contents of the str_set1 before swap operation: White Yellow Blue Red Content of the str_set2 before swap operation: Mango Banana Orange Apple Contents of the str_set1 after swap operation: Mango Banana Orange Apple Content of the str_set2 after swap operation: White Yellow Blue Red