Home » C++ multiset swap() function

C++ multiset swap() function

by Online Tutorials Library

C++ multiset swap()

C++ Multiset swap() function is used to swap (or exchange) the contents of two multisets but both the multisets must be of same type although sizes may differ.

Syntax

Parameter

x: multiset container to exchange the contents with.

Return value

None

Complexity

Constant.

Iterator validity

All references, iterators and pointers referring to elements in both multiset containers remain valid, but now are referring to elements in the other multiset container, and iterate on it.

Data Races

Both the container and x are modified.

Exception Safety

No effect on container if exception is thrown.

Example 1

Let’s see the simple example to swap the element of one multiset to another:

Output:

Multiset m2 contains following elements  1  2  3  3  4  5  

In the above example, multiset m1 has five elements and m2 is empty. When you swap m1 to m2 then all the elements of m1 is swapped to m2.

Example 2

Let’s see a simple example to exchange the contents of two multisets:

Output:

first contains: 20 20 36  second contains: 4 19 72  

Example 3

Let’s see a simple example to swap the contents of two multisets:

Output:

multiset1:  x  y  y  z  multiset2:  a  b  c  c  d  

In the above example, another form of swap() function is used to swap the contents of two multisets.

Example 4

Let’s see a simple example:

Output:

The original multiset s1 is: 10 10 20.  After swapping with s2, multiset s1 is: 100 200.  After swapping with s3, multiset s1 is: 200.  
Next TopicC++ multiset

You may also like