Check if a Value exists in a Map in C++

This tutorial will discuss how to check if a value exists in a map in C++.

To check if a map contains a value or not, we can iterate over all the key-value pairs of the map using a range-based for loop, and then for each pair, we can compare the second value of the pair with the given value. If a match is found, then it means that the map contains the given value. We have created a generic function for this,

template <typename K, typename V>
bool containsValue(
            const std::map<K, V> &mapObj,
            const V &value)
{
    bool result = false;
    for (const auto &pair : mapObj)
    {
        if (pair.second == value)
        {
            result = true;
            break;
        }
    }
    return result;
}

We can pass a map of any type as the first argument and as the second argument, we can pass a value. It will return true if the map contains any occurrence of the given value in its value fields.

Suppose we have a map of word frequency with keys of string type and values of integer type.

std::map<std::string, int> wordFrequency{
                                    {"this", 22},
                                    {"why", 33},
                                    {"what", 67},
                                    {"how", 41} };

This map basically contains the frequency count of each word in a text. Now we want to check if a given value like 67 exists in this map or not. For this, we can use our generic function containsValue(), and we will pass the map and the value as arguments in it.

int value = 67;
if (containsValue(wordFrequency, value))
{
    std::cout << "Value exists in the map." << std::endl;
}
else
{
    std::cout << "Value does not exist in the map." << std::endl;
}

If the containsValue() function returns true, it means that the given value exists in the map.

Let’s see the complete example,

#include <iostream>
#include <map>

template <typename K, typename V>
bool containsValue(
            const std::map<K, V> &mapObj,
            const V &value)
{
    bool result = false;
    for (const auto &pair : mapObj)
    {
        if (pair.second == value)
        {
            result = true;
            break;
        }
    }
    return result;
}

int main()
{
    std::map<std::string, int> wordFrequency{
                                        {"this", 22},
                                        {"why", 33},
                                        {"what", 67},
                                        {"how", 41} };

    int value = 67;
    if (containsValue(wordFrequency, value))
    {
        std::cout << "Value exists in the map." << std::endl;
    }
    else
    {
        std::cout << "Value does not exist in the map." << std::endl;
    }

    return 0;
}

Output

Value exists in the map.

Summary

Today, we learned how to check if a value exists in a map in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top