map count() function in C++ STL
Last Updated :
12 Jul, 2018
The map::count() is a built-in function in C++ STL which returns 1 if the element with key K is present in the map container. It returns 0 if the element with key K is not present in the container.
Syntax:
map_name.count(key k)
Parameters: The function accepts a mandatory parameter k which specifies the key to be searched in the map container.
Return Value: The function returns the number of times the key K is present in the map container. It returns 1 if the key is present in the container as the map only contains a unique key. It returns 0 if the key is not present in the map container.
Below program illustrates the above function:
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , int > mp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 4, 20 });
mp.insert({ 5, 50 });
if (mp.count(1))
cout << "The key 1 is present\n" ;
else
cout << "The key 1 is not present\n" ;
if (mp.count(100))
cout << "The key 100 is present\n" ;
else
cout << "The key 100 is not present\n" ;
return 0;
}
|
Output:
The key 1 is present
The key 100 is not present
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...