Home »
C++ STL
unordered_map count() Function in C++ with Examples
C++ unordered_map count() Function: Here, we will learn about the count() function, its usages, syntax and examples.
Submitted by Shivang Yadav, on July 20, 2022
unordered_map:
An unordered_map is a special type of container that stores data in the form of key-value pairs in an unordered manner. The key stored is used to identify the data value mapped to it uniquely.
std::unordered_map::count() Function
The count() function is used to return the count of the number of elements present in the unordered_map with the given key value.
The method returns two values either 0 or 1 as the map does not allow duplicate keys. Hence, we can say that the method is used to check if the unordered_map contains an element with a given key or not.
std::unordered_map::count() Function Syntax:
unordered_map.count(k);
Parameter(s): It accepts a single value which is the key value whose occurrence count is to be found.
Return Value: It returns an integer value marking the presence or absence of key elements in the map.
C++ unordered_map count() Function Example
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
using namespace std;
int main()
{
// Creating unordered_map
unordered_map<int, string> unorderedMap;
unorderedMap = { { 1, "Scala" }, { 2, "Python" }, { 3, "JavaScript" }, { 4, "C++" } };
cout << "The count of elements in map, with key value 4 : " << unorderedMap.count(4) << endl;
cout << "The count of elements in map, with key value 7 : " << unorderedMap.count(7);
return 0;
}
Output:
The count of elements in map, with key value 4 : 1
The count of elements in map, with key value 7 : 0