I'm relatively new to STL and particularly algorithm library.
I'm trying to iterate over a std::multimap, where both key and value are of type std::string, using std::for_each() function and perform some task.
Here's my std::multimap:
std::multimap<std::string, std::string> myMap;
for (int i = 0; i < 5; i++) {
myMap.insert(std::make_pair("key", "value"));
}
And here is the line with for_each() function:
std::for_each(myMap.begin(), myMap.end(), ask);
The ask() function is defined as following:
void ask(std::pair<std::string, std::string>& entry) {
// do something...
}
The program gives me the following error:
error: invalid initialization of reference of type 'std::__cxx11::basic_string&' from expression of type 'std::pair, std::__cxx11::basic_string >'
I know that, the problem is on the line with for_each() function and I assume it's because the parameter of ask() function is not correct.
But to my knowledge, each entry of the std::multimap is represented as a std::pair, so shouldn't the ask() function take a std::pair object as its parameter?