C++ multiset operator=
There are following three uses of operator= in multiset:
- operator= is used to assign new content to the multiset container by replacing its old content (or copy the content) and modifies size if necessary.
- operator= is used to move the content of one multiset container into another and modifies size if necessary.
- operator= is used to copy the elements from initializer list to multiset container.
Syntax
copy (1):- Copies all the elements from x into the multiset container.
move (2):- Moves the contents of x into the multiset container.
initializer_list (3):- Copies the elements of il into the multiset container.
Parameter
x: A multiset object with the same type.
il: An initializer list object.
Return value
this pointer.
Complexity
Copy assignment: Linear in sizes.
Move assignment: Linear in current container size.
Initializer list assignment: Up to logarithmic in sizes.
Iterator validity
All references, iterators and pointers related to this multiset container are invalidated.
Data Races
All copied elements are accessed.
The move assignment modifies x.
The multiset container and all its elements are modified.
Exception Safety
If an exception is thrown, the container is in a valid state.
Example 1
Let’s see the simple example to copy the content of one multiset to another:
Output:
Multiset s1 contains following elements 10 10 20 30 After copying the elements from s1 to s2... Multiset s2 contains following elements 10 10 20 30
In the above example, operator = is used to copy the content of one multiset s1 to another multiset s2.
Example 2
Let’s see a simple example to move the elements of one multiset to another:
Output:
Multiset m1 contains following elements a, e, e, i, o, u, u, After moving the elements from s1 to s2... Multiset s2 contains following elements a, e, e, i, o, u, u,
In the above example, operator = is used to move the content of one multiset s1 to another multiset s2.
Example 3
Let’s see a simple example to copy the content from initializer list to multiset:
Output:
Multiset contains the following elements 100 100 200 300 300
In the above example, operator = is used to copy the content from initializer list to multiset m.
Example 4
Let’s see a simple example:
Output:
Size Of c1:0 Size Of c2:7
In the above example, there are two multisets c1 and c2. c1 has 7 elements and c2 is empty, but after assigning c1 to c2, size of c1 become 0 and size of c2 become 7.