In this article, we’ll learn how to check if an unordered_map in C++ contains multiple keys that are provided in a vector.

The objective is to determine if an unordered_map contains a specific set of keys. These keys are given in a vector. We need to verify whether each key in the vector exists in the map.

Suppose we have an unordered_map like this:

std::unordered_map<std::string, int> wordFrequency = {
    {"Where", 20},
    {"How", 21},
    {"That", 19}
};

And a vector of keys to check:

std::vector<std::string> keysToCheck = {"How", "What", "Where"};

We aim to find out if each key in keysToCheck is present in unordered_map object wordFrequency or not.

Technique 1: Using C++20 Contains() function

In C++20, we can use the contains() method of unordered_map along with the std::all_of algorithm from the <algorithm> header to check if all keys in a vector exist in an unordered_map.

Let’s see the complete example,

#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    std::unordered_map<std::string, int> wordFrequency = {
        {"Where", 20},
        {"How", 21},
        {"That", 19}
    };
    std::vector<std::string> keysToCheck = {"How", "What", "Where"};

    bool allKeysExist = std::all_of(
        keysToCheck.begin(), keysToCheck.end(),
        [&wordFrequency](const std::string& key) {
            return wordFrequency.contains(key);
        }
    );

    if (allKeysExist) {
        std::cout << "All keys exist in the map." << std::endl;
    } else {
        std::cout << "Not all keys exist in the map." << std::endl;
    }

    return 0;
}

Output

Not all keys exist in the map.

In this code, std::all_of iterates over each element in keysToCheck. The lambda function checks if the current key exists in wordFrequency using the contains() method. If all keys exist (std::all_of returns true), it prints that all keys are present in the map. Otherwise, it indicates that not all keys exist.

Technique 2: Iterative Check with find()

We can iterate over the vector and use the find() method of unordered_map to check if each key exists.

Let’s see the complete example,

#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>

bool containsAll(
        std::unordered_map<std::string, int>& wordFrequency,
        std::vector<std::string>& keysToCheck)
{
    bool result = true;
    for (const auto& key : keysToCheck)
    {
        if (wordFrequency.find(key) == wordFrequency.end())
        {
           bool result = false;
           break;
        }
    }
    return result;
}

int main()
{
    std::unordered_map<std::string, int> wordFrequency = {
        {"Where", 20},
        {"How", 21},
        {"That", 19}
    };
    std::vector<std::string> keysToCheck = {"How", "What", "Where"};

    if (containsAll (wordFrequency, keysToCheck) )
    {
        std::cout << "All Keys Found " << std::endl;
    }
    else
    {
        std::cout << "All Keys are not present in Unordered_map" << std::endl;
    }

    return 0;
}

Output

All Keys Found

In this example, we loop through each key in keysToCheck. For each key, we use the find() method of wordFrequency to check its existence in the map. If find() returns an iterator to the end of the map, the key does not exist; otherwise, it does.

Summary

The contains() and std::all_of in C++20 offers an elegant and efficient way to check if all keys in a vector are present in an unordered_map. However, if you are using older version of C++, then you can iteratively use the find() method for each key.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

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