Javatpoint Logo
Javatpoint Logo

C++ set size()

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

Syntax

Member type size_type is an unsigned integral type.

Parameter

None

Return value

It returns the number of elements present in the set.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of a set is safe.

Exception Safety

This function never throws exception.

Example 1

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

Output:

 num set contains 4 elements.

In the above example, set num contains 4 elements. Therefore size() returns 4 elements.

Example 2

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

Output:

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

In the above example, first set 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
300
400

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

Example 4

Let's see a simple example:

Output:

Enter three sets of marks: 
78 90 84

Size of phone set is: 3
List of telephone numbers: 
78 
 84 
 90

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






Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA