×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

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

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.

C++ STL 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.

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.

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.