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).
SyntaxFollowing 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
This function returns the (*this) unordered_set container.
Example 1In 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 1Example 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 1Example 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4