![]() |
VOOZH | about |
The std::map::count() in C++ is a built-in function that is used to count the occurrence of the given key in the map container. It is the member function of the std::map container.
In this article, we will learn how to use std::map::count() in C++.
mp.count(key)
Count of 1: 1 Count of 9: 0
Time Complexity: O(log n), where n is the number of key in the map container.
Auxiliary Space: O(1)
std::map() container does not store the duplicate values, so why map::count() is needed as we can already check the existence of an element in a map using std::find() or map::find() functions.
Other containers such as multimap may contain more than one copy of the key. So the reason is that the containers in C++ STL are required to implement the count() function is to help in generic programming.