- 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::operators=() function
The C++ std::unordered_set::operator=() function is used to replace the content of the unordered_set container or to copy or move an unordered_set to another unordered_set container.
The assignment operator is only defined for objects of the same type; if we try to attempt the assignment operator with different types, it will result in a compilation error.
This function has 3 polymorphic variants: with using the copy assignment operator, move assignment operator and initializer list(you can find the syntaxes of all the variants below).
Syntax
Following is the syntax of std::operators=() function.
unordered_set& operator= ( const unordered_set& ust ); or unordered_set& operator= ( unordered_set&& ust ); or unordered_set& operator= ( intitializer_list<value_type> il );
Parameters
- ust − It indicates the unordered_set object of the same type.
- il − It indicates an initializer_list object.
Return Value
This function returns the (*this) unordered_set container.
Example 1
In the following example, we are going to use the unordered_set::operator=() function to copy the elements of the current unordered_set into the other unordered_set.
#include <unordered_set>
#include <iterator>
#include <iostream>
using namespace std;
int main() {
unordered_set<int> uSet, myUset;
uSet = {1, 2, 3 ,4, 5};
//assigning the current unordered_set into another unordrer_set
myUset = uSet;
cout<<"Elements of myUset: ";
for(auto it: myUset){
cout<<it<<" ";
}
cout<<endl;
return 0;
}
Output
If we run the above code it will generate the following output −
Elements of myUset: 5 4 3 2 1
Example 2
Consider the following example, where we are going to use the move version of the std::unordered_map::operator=() function.
#include <unordered_set>
#include <iterator>
#include <iostream>
using namespace std;
int main() {
unordered_set<int> uSet, myUset;
uSet = {1, 2, 3 ,4, 5};
//assigning the current unordered_set into another unordrer_set
myUset = move(uSet);
cout<<"Elements of myUset: ";
for(auto it: myUset){
cout<<it<<" ";
}
cout<<endl;
return 0;
}
Output
Following is the output of the above code −
Elements of myUset: 5 4 3 2 1
Example 3
Let's look at the following example, where we are going to replace the content of ilist in the current unordered_set.
#include <iostream>
#include <unordered_set>
using namespace std;
int main(void) {
unordered_set<char> uSet = { 'a', 'b', 'c', 'd', 'e'};
auto ilist = {'A', 'B', 'C'};
cout<<"uSet content before the assignment operator= operation: "<<endl;
for(auto&it : uSet){
cout<<it<<endl;
}
cout<<"The ilist content before the assignment operator = operation: "<<endl;
for(auto & i : ilist){
cout<<i<<endl;
}
//using operator = function
uSet = ilist;
cout<<"uSet content after the assignment operator = operation: "<<endl;
for(auto& it : uSet){
cout<<it<<endl;
}
return 0;
}
Output
Output of the above code is as follows −
uSet content before the assignment operator= operation: e d c b a The ilist content before the assignment operator = operation: A B C uSet content after the assignment operator = operation: C B A