set operator= in C++ STL - GeeksforGeeks

02 Jun, 2020
The ‘=’ is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function:
The first version takes reference of an set as an argument and copies it to an set.Syntax:ums1.operator=(set &set2)Parameters: The first version takes the reference of an set as argument.The second version performs a move assignment i.e it moves the content of an set to another set.Syntax:ums1.operator=(set &&set2)Parameters: The second version takes the r-value reference of an set as argumentThe third version assigns contents of an initializer list to an set.Syntax:ums1.operator=(initializer list)Parameters: The third version takes an initializer list as argument.Return Value: All of them return the value of this pointer(*this).The following programs illustrates set::operator= :// C++ code to illustrate the method// set::operator=()  #include <iostream>#include <set>using namespace std;  // merge functiontemplate <class T>T merge(T a, T b){    T t(a);    t.insert(b.begin(), b.end());    return t;}  int main(){    set<int> sample1, sample2, sample3;      // List initialization    sample1 = { 1, 2, 3, 4, 5 };    sample2 = { 6, 7, 8, 1 };      // Merge both sets and    // move the result to sample3    sample3 = merge(sample1, sample2);      // copy assignment    sample1 = sample3;      // Print the sets    for (auto it = sample1.begin(); it != sample1.end(); ++it) {        cout << *it << " ";    }      cout << endl;      for (auto it = sample2.begin(); it != sample2.end(); ++it) {        cout << *it << " ";    }      cout << endl;      for (auto it = sample3.begin(); it != sample3.end(); ++it) {        cout << *it << " ";    }      cout << endl;      return 0;}Output:1 2 3 4 5 6 7 8 
1 6 7 8 
1 2 3 4 5 6 7 8
My Personal Notes
arrow_drop_upSave
The first version takes reference of an set as an argument and copies it to an set.Syntax:ums1.operator=(set &set2)Parameters: The first version takes the reference of an set as argument.
Syntax:
ums1.operator=(set &set2)
Parameters: The first version takes the reference of an set as argument.
The second version performs a move assignment i.e it moves the content of an set to another set.Syntax:ums1.operator=(set &&set2)Parameters: The second version takes the r-value reference of an set as argument
Syntax:
ums1.operator=(set &&set2)
Parameters: The second version takes the r-value reference of an set as argument
The third version assigns contents of an initializer list to an set.Syntax:ums1.operator=(initializer list)Parameters: The third version takes an initializer list as argument.Return Value: All of them return the value of this pointer(*this).The following programs illustrates set::operator= :// C++ code to illustrate the method// set::operator=()  #include <iostream>#include <set>using namespace std;  // merge functiontemplate <class T>T merge(T a, T b){    T t(a);    t.insert(b.begin(), b.end());    return t;}  int main(){    set<int> sample1, sample2, sample3;      // List initialization    sample1 = { 1, 2, 3, 4, 5 };    sample2 = { 6, 7, 8, 1 };      // Merge both sets and    // move the result to sample3    sample3 = merge(sample1, sample2);      // copy assignment    sample1 = sample3;      // Print the sets    for (auto it = sample1.begin(); it != sample1.end(); ++it) {        cout << *it << " ";    }      cout << endl;      for (auto it = sample2.begin(); it != sample2.end(); ++it) {        cout << *it << " ";    }      cout << endl;      for (auto it = sample3.begin(); it != sample3.end(); ++it) {        cout << *it << " ";    }      cout << endl;      return 0;}Output:1 2 3 4 5 6 7 8 
1 6 7 8 
1 2 3 4 5 6 7 8
My Personal Notes
arrow_drop_upSave
Syntax:
ums1.operator=(initializer list)
Parameters: The third version takes an initializer list as argument.
Return Value: All of them return the value of this pointer(*this).
The following programs illustrates set::operator= :
// C++ code to illustrate the method// set::operator=()  #include <iostream>#include <set>using namespace std;  // merge functiontemplate <class T>T merge(T a, T b){    T t(a);    t.insert(b.begin(), b.end());    return t;}  int main(){    set<int> sample1, sample2, sample3;      // List initialization    sample1 = { 1, 2, 3, 4, 5 };    sample2 = { 6, 7, 8, 1 };      // Merge both sets and    // move the result to sample3    sample3 = merge(sample1, sample2);      // copy assignment    sample1 = sample3;      // Print the sets    for (auto it = sample1.begin(); it != sample1.end(); ++it) {        cout << *it << " ";    }      cout << endl;      for (auto it = sample2.begin(); it != sample2.end(); ++it) {        cout << *it << " ";    }      cout << endl;      for (auto it = sample3.begin(); it != sample3.end(); ++it) {        cout << *it << " ";    }      cout << endl;      return 0;}
1 2 3 4 5 6 7 8 
1 6 7 8 
1 2 3 4 5 6 7 8

ShilpaDeshmukh
cpp-set
Picked
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Inline Functions in C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++