The set::count() is a built-in function in C++ STL which returns the number of times an element occurs in the set. It can only return 1 or 0 as the set container contains unique elements only.
Syntax:
set_name.count(element)
Parameters: The function accepts one mandatory parameter element which specifies the element whose count is to be returned.
Return Value: The function returns 1 or 0 as the set contains unique elements only. It returns 1 if the value is present in the set container. It returns 0 if it is not present in the container.
Below is the illustration of the above function.
// CPP program to demonstrate the // set::count() function #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 14, 12, 15, 11, 10 }; // initializes the set from an array set<int> s(arr, arr + 5); // check if 11 is present or not if (s.count(11)) cout << "11 is present in the set\n"; else cout << "11 is not present in the set\n"; // checks if 18 is present or not if (s.count(18)) cout << "18 is present in the set\n"; else cout << "18 is not present in the set\n"; return 0; } |
11 is present in the set 18 is not present in the set
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- map count() function in C++ STL
- unordered_set count() function in C++ STL
- unordered_multimap count() function in C++ STL
- multiset count() function in C++ STL
- unordered_multiset count() function in C++ STL
- Count the number of objects using Static member function
- Difference between user defined function and library function in C/C++
- How to call some function before main() function in C++?
- Function Overloading vs Function Overriding in C++
- std::count() in C++ STL
- multimap::count() in C++ STL
- unordered_map count() in C++
- bitset count() in C++ STL
- Count substrings that contain all vowels | SET 2
- Count numbers having 0 as a digit
- Count smaller elements on right side using Set in C++ STL
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- Count the pairs of vowels in the given string
- Count number of unique Triangles using STL | Set 1 (Using set)
- Count number of triplets in an array having sum in the range [a, b]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

