C++ multiset size()

C++ Multiset size() function is used to find the number of elements present in the multiset container.

Syntax

Member type size_type is an unsigned integral type.

Parameter

None

Return value

The size() function returns the number of elements present in the multiset.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of a multiset container is safe.

Exception Safety

This function never throws exception.

Example 1

Let's see the simple example to calculate the size of the multiset:

Output:

num multiset contains 5 elements.

In the above example, multiset num contains 5 elements. Therefore size() returns 5 elements.

Example 2

Let's see a simple example to calculate initial size of multiset and size of multiset after adding elements:

Output:

Initial size of multiset = 0
Size of multiset after inserting elements = 6

In the above example, first multiset is empty hence, size() function will return 0 and after inserting 6 elements it will return 6.

Example 3

Let's see a simple example:

Output:

100
200
200
300
400

In the above example, It simply uses the size() function in while loop and prints the elements of multiset until the size of multiset.

Example 4

Let's see a simple example:

Output:

Enter three sets of marks: 
340
235
340

Size of marks multiset is: 3
List of Marks: 
235 
340 
340

In the above example, the program first creates marks multiset interactively. Then it displays the total size of marks multiset and all the elements available in the multiset.

Next TopicC++ multiset