How to Find the Size of a Set in C++? (original) (raw)

Last Updated : 12 Feb, 2024

In C++, sets are STL containers that store unique elements of the same type in a sorted manner. No duplicate elements are allowed in the sets, as the value of every element in a set is unique. In this article, we will learn how we can find the size of a set container in C++.

**Example:

**Input: mySet={1,2,3,4,5,6,7,8}

**Output: The size of mySet is: 8

Find the Size of a Set in C++

The size of the set container refers to the number of elements currently present in it. The std::set container provides a member function named std:📐:size() which returns the size of the set or the number of elements in the set.

**Syntax of std:📐:size()

_setname.**size()

**C++ Program to Find the Size of a Set

C++ `

// C++ program to Find the Size of a Set in C++ #include #include

using namespace std;

int main() { // initialize a set set s = { 1, 2, 3, 4, 5, 6, 7, 8 };

// Find the size of the set using the size() function
cout << "Size of the set is : " << s.size() << endl;

return 0;

}

`

Output

Size of the set is : 8

**Time Complexity : O(1)
**Auxilary Space: O(1)