C++ set operator=
There are following three uses of operator = in set:
- Operator = is used to assign new content to the set container by replacing its old content (or copy the content ) and modifies size if necessary.
- Operator = is used to move the content of one set container into another and modifies size if necessary.
- Operator = is used to copy the elements from initializer list to set container.
Syntax
copy(1) set& operator= (const set& x); //until C++ 11
copy (1) set& operator= (const set& x); //since C++ 11
move (2) set& operator= (set&& x); //since C++ 11
initializer list (3) set& operator= (initializer_list<value_type> il); //since C++ 11
Duplicates all the items from x to the set collection.
Shifting (2) :- Transfers the data from x to the set container.
Copies the elements from the initializer_list il into the set container.
Parameter
x : A set object with the same type.
il : An initializer list object.
Return value
this pointer.
Complexity
Copy assignment : Linear in sizes.
Relocating elements: Linear in the size of the existing container.
Initializer list assignment: Efficient for data structures with sizes up to logarithmic scale.
Iterator validity
All references, iterators, and pointers associated with this collection become invalid.
Data Races
All copied elements are accessed.
The move assignment modifies x.
The set container and its contents are altered.
Exception Safety
If a program triggers an exception, the container remains in a valid state.
Example 1
Let's explore a basic illustration of transferring the contents from one set to another:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
set<int> s1 = {10,20,30};
cout << "Set s1 contains following elements" << endl;
for (auto it = s1.begin(); it != s1.end(); ++it)
cout << *it << endl;
set<int> s2 = s1;
cout<<"\nAfter Copying the elements from s1 to s2... \n";
cout << "\nSet s2 contains following elements" << endl;
for (auto it = s2.begin(); it != s2.end(); ++it)
cout << *it<< endl;
return 0;
}
Output:
Set s1 contains following elements
10
20
30
After copying the elements from s1 to s2...
Set s2 contains following elements
10
20
30
In the provided illustration, the operator = is employed to duplicate the contents of a set s1 into another set s2.
Example 2
Let's examine a basic illustration of transferring elements from one set to another:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
set<char> s1 = {'a','e','i','o','u'};
cout << "Set m1 contains following elements" << endl;
for (auto it = s1.begin(); it != s1.end(); ++it)
cout << *it << ", ";
set<char> s2 = move(s1);
cout<<"\n\nAfter moving the elements from s1 to s2... \n";
cout << "\nSet s2 contains following elements" << endl;
for (auto it = s2.begin(); it != s2.end(); ++it)
cout << *it << ", ";
return 0;
}
Output:
Set m1 contains following elements
a, e, i, o, u,
After moving the elements from s1 to s2?
Set s2 contains following elements
a, e, i, o, u,
In the provided instance, the operator = is employed to transfer the elements from set s1 to set s2.
Example 3
Let's explore a basic example demonstrating the transfer of data from an initializer list to a set:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
set<int> s;
s = {100, 200, 300, 400, 500}; //initializer list
cout << "Set contains the following elements" << endl;
for (auto it = s.begin(); it != s.end(); ++it)
cout << *it << endl;
return 0;
}
Output:
Set contains the following elements
100
200
300
400
500
In the example provided, the operator = is employed to transfer the data from the initializer list to populate the set m.
Example 4
Let's see a simple example:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
int values [] = { 5 , 2 , 4 , 1 , 0 , 0 , 9 };
set < int > c1 ( values , values + 7 );
set < int > c2 ;
c2 = c1 ;
c1 = set < int > ();
cout<< "Size Of c1:" << c1 . size () << endl ;
cout<< "Size Of c2:" << c2 . size () << endl ;
}
Output:
Size Of c1:0
Size Of c2:6
In the provided scenario, we have two collections named c1 and c2. Initially, c1 contains 7 elements while c2 is devoid of any elements. However, upon transferring the contents of c1 to c2, the size of c1 transforms to 0, and simultaneously, the size of c2 increases to 7.