Home » C++ multiset clear() function

C++ multiset clear() function

by Online Tutorials Library

C++ multiset clear()

C++ Multimultiset clear() function is used to remove all the elements of the multiset container. It clears the multiset and converts its size to 0.

Syntax

Parameter

None

Return value

None

Complexity

Linear in size.

Iterator validity

All pointers, references and iterator related to this container are invalidated.

Data Races

The container is modified.

All contained elements are modified.

Exception Safety

This function never throws exception.

Example 1

Let’s see the simple example to calculate the size of multiset before and after clear operation:

Output:

Initial size of multiset before clear operation = 4  Size of multiset after clear operation = 0  

In the above example, a multiset is initialized with 4 elements therefore, the size is 4 but after the clear operation, size becomes 0.

Example 2

Let’s see a simple example to clear the elements of the multiset:

Output:

mymultiset contains:  Aman  Deep  Nikita    mymultiset contains:  Deep  Raaz  

In the above example, after clear the multiset, we can add the new elements without initialization.

Example 3

Let’s see a simple example to clear the elements of the multiset:

Output:

m1 group has following members:  Hello World     m2 group has following members:  Java Program     m3 group has following members:  C++ Coding     Which group do you want to delete?   1.m1   2.m2   3.m3   Please enter your choice: 2    Group m2 has been cleared.  

In the above example, there are three groups of multiset and according to the user?s choice one group has been deleted.

Example 4

Let’s see a simple example:

Output:

1.  Fruit bucket has following fruits =   Apple  Banana  Banana  Orange    Do you want to clear your fruit bucket?  Press 1 for Yes and 0 for No: 1  0 fruits in bucket       2.   Fruit bucket has following fruits =   Apple   Banana   Banana  Orange    Do you want to clear your fruit bucket?  Press 1 for Yes and 0 for No: 1  4 fruits in bucket  

In the above example, a fruit multiset is initialized with three fruits. Asking for clear the multiset if you enter 0 then fruit bucket has 4 elements or if you enter 1 then it will clear the fruit multiset and the size becomes 0.

Next TopicC++ multiset

You may also like