In this article will discuss how to search for all the elements in map with given value.
Map internally store elements in Key-Value pair. In which keys are unique but values can be duplicate. There are member functions to search pairs by key i.e. std::map::find(). But there is no direct function to search for all the elements with given value.
Suppose we have map of string & int i.e.
// Map of string & int i.e. words as key & there // occurrence count as values std::map<std::string, int> wordMap = { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 } };
Now we want to find all the elements with value 6 i.e.
at :: 6
is :: 6
To do this we need to create a customized function.
Algorithm Used:
As there is no direct function so, we need to iterate over all the elements and check the value of each element. If value matches the given value then add them to the list.
Let’s create a generic function for it i.e.
/* * Generic implementation to search if a given value exists in a map or not. * Adds all the keys with given value in the vector */ template<typename K, typename V> bool findByValue(std::vector<K> & vec, std::map<K, V> mapOfElemen, V value) { bool bResult = false; auto it = mapOfElemen.begin(); // Iterate through the map while(it != mapOfElemen.end()) { // Check if value of this entry matches with given value if(it->second == value) { // Yes found bResult = true; // Push the key in given map vec.push_back(it->first); } // Go to next entry in map it++; } return bResult; }
It accepts a vector of key type by reference, the map to search and the value to look for. Then it pushes all the keys with given value in the vector.
Let’s call this function to search for all the pairs with value 6 i.e.
// Create a vector of string std::vector<std::string> vec; // Find all the keys with value 6 bool result = findByValue(vec, wordMap, 6);
Complete example is as follows,
Pointers in C/C++ [Full Course]
#include <iostream> #include <map> #include <string> #include <iterator> #include <vector> /* * Generic implementation to search if a given value exists in a map or not. * Adds all the keys with given value in the vector */ template<typename K, typename V> bool findByValue(std::vector<K> & vec, std::map<K, V> mapOfElemen, V value) { bool bResult = false; auto it = mapOfElemen.begin(); // Iterate through the map while(it != mapOfElemen.end()) { // Check if value of this entry matches with given value if(it->second == value) { // Yes found bResult = true; // Push the key in given map vec.push_back(it->first); } // Go to next entry in map it++; } return bResult; } int main() { // Map of string & int i.e. words as key & there // occurrence count as values std::map<std::string, int> wordMap = { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 } }; // Create a vector of string std::vector<std::string> vec; int value = 6; // Find all the keys with value 6 bool result = findByValue(vec, wordMap, value); if(result) { std::cout<<"Keys with value "<< value<< " are,"<<std::endl; for(auto elem : vec) { std::cout<<elem<<std::endl; } } else { std::cout<<"No Key is found with the given value"<<std::endl; } return 0; }
Output:
Keys with value 6 are, at is
Pingback: std::map Tutorial Part 1: Usage Detail with examples – thisPointer.com