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:
// C++ program to illustrate // the map::count() function #include <bits/stdc++.h> using namespace std; int main() { // initialize container map<int, int> mp; // insert elements in random order mp.insert({ 2, 30 }); mp.insert({ 1, 40 }); mp.insert({ 3, 60 }); mp.insert({ 4, 20 }); mp.insert({ 5, 50 }); // checks if key 1 is present or not if (mp.count(1)) cout << "The key 1 is present\n"; else cout << "The key 1 is not present\n"; // checks if key 100 is present or not if (mp.count(100)) cout << "The key 100 is present\n"; else cout << "The key 100 is not present\n"; return 0; } |
The key 1 is present The key 100 is not present
Recommended Posts:
- set count() function in C++ STL
- unordered_set count() function in C++ STL
- multiset count() function in C++ STL
- unordered_multiset count() function in C++ STL
- unordered_multimap count() function in C++ STL
- Count the number of objects using Static member function
- Function Overloading vs Function Overriding in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- std::count() in C++ STL
- multimap::count() in C++ STL
- bitset count() in C++ STL
- unordered_map count() in C++
- Count numbers having 0 as a digit
- Count number of unique Triangles using STL | Set 1 (Using set)
- Count smaller elements on right side using Set in C++ STL
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.



