# Set count() function in C++ STL

C++Server Side ProgrammingProgramming

In this article we are going to discuss the set::count in C++ STL, their syntax, working and their return values.

## What is Set in C++ STL?

Sets in C++ STL are the containers which must have unique elements in a general order. Sets must have unique elements because the value of the element identifies the element. Once added a value in set container later can’t be modified, although we can still remove or add the values to the set. Sets are used as binary search trees.

## What is set::count()?

count() function is an inbuilt function in C++ STL, which is defined in header file. count() is used to count the number of times an argument is found in a set associated with the function. This function can return only two values 0 or 1 because in a set all the values are unique, so at most a value in the set will occur once.

## Syntax

name_of_set.count(const type_t& value);

## Parameter

This function accepts only 1 parameter, i.e the value which we want to find and count in the set container

## Return value

This function can return only two values, either 0 (the value is not present in the container), or 1 (the value is present in the container).

## Example

Input: set <int> myset = {1, 2, 3, 4, 6};
myset.count(2);
Output: 1
Input: set<int> myset = {1, 2, 3, 4, 6};
myset.count(5);
Output: 0

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {2, 4, 2, 5, 6, 7};
set<int> ch(arr, arr + 6);
// check if 2 is present
if (ch.count(2))
cout<<"2 is present\n";
else
cout<<"2 is not present\n";
// checks if 4 is present
if (ch.count(9))
cout<<"9 is present\n";
else
cout<<"9 is not present\n";
return 0;
}

## Output

If we run the above code it will generate the following output

2 is present
9 is not present