map count( ) function in C++

In this article we will be discussing the working, syntax and examples of map::empty() function in C++ STL.
Maps are the associative container, which facilitates to store the elements formed by a combination on key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in map container is accessed by its unique keys.
The map::count( ) is a function which comes under <map> header file. This function counts the elements with specific key, it returns 1 if the element with key is present, It returns the 0 if the element with key is not present in container.
map_name.count(key n);
This function accepts the parameter N which specifies Key in container.
This function returns the Boolean Number 1 if the key is present in the container and returns 0 if the key is not present in the container.
Input (key, Element) 
(2,70), (3,30), (4,90), (5,100)
Output 
Key 5 is present.
Key 6 is not present.
2,11
3,26
1,66
4,81
Key 2 is present.
Key 8 is not present.
First initialize the container.
First initialize the container.
Then insert the element with their keys.
Then insert the element with their keys.
Then we check the desired key is present in the container.
Then we check the desired key is present in the container.
By using the above approach, we can check the key is present in the container, there is another approach which can be followed as −
Firstly, we initialize the container.
Firstly, we initialize the container.
Then we insert the element with their keys.
Then we insert the element with their keys.
Then we create the loop from first element to last element.
Then we create the loop from first element to last element.
In this loop, we check the the desired key is present or not.
In this loop, we check the the desired key is present or not.
Above approach is generally used for alphabetically stored element. In this approach code print that the element is present in element or not in Output.
/ / C++ code to demonstrate the working of map count( ) function
#incude<iostream.h>
#include<map.h>
Using namespace std;
int main( ){
   map<int, int> mp;
   mp.insert({1, 40});
   mp.insert({3, 20});
   mp.insert({2, 30});
   mp.insert({5, 10});
   mp.insert({4, 50});
   if (mp.count(1))
      cout<< ” The Key 1 is present\n”;
   else
      cout<< ” The Key 1 is not present\n”;
   if(mp.count(7))
      cout<< “ The Key 7 is Present \n”;
   else
      cout<< “ The Key 7 is not Present\n”;
   return 0;
}
If we run the above code it will generate following output
The Key 1 is present
The Key 7 is not present
#include<iostream.h>
#include<map.h>
Using namespace std;
int main ( ){
   map<char, int> mp;
   int I;
   mp[‘a’] = 2;
   mp[‘c’] = 3;
   mp[‘e’] = 1;
   for ( i = ’a’ ; i < ’f’ ; i++){
      cout<< i;
      if (mp.count(i)>0)
         cout<< “ is an element of mp.\n”;
      else
         cout<< “ is not an element of mp.\n”;
   }
   return 0;
}
If we run the above code it will generate the following output
a is an element of mp.
b is not an element of mp.
c is an element of mp.
d is not an element of mp.
e is an element of mp.
f is not an element of mp.