How to Find the Size of a Set in C++?
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::set::size() which returns the size of the set or the number of elements in the set.
Syntax of std::set::size()
set_name.size()
C++ Program to Find the Size of a Set
C++
#include <iostream>
#include <set>
using namespace std;
int main()
{
set< int > s = { 1, 2, 3, 4, 5, 6, 7, 8 };
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)
Share your thoughts in the comments
Please Login to comment...