Let's say I insert the string "hello" and the int 0 into my map:
exampleMap.insert(std::make_pair("hello", 0));
Is there any way to print the "hello" string?
you need to iterate to find the entries having the expected value to print the key
#include <map>
#include <string>
#include <iostream>
int main()
{
std::map<std::string, int> exampleMap;
exampleMap.insert(std::make_pair("hello", 0));
exampleMap.insert(std::make_pair("how", 1));
exampleMap.insert(std::make_pair("are", 2));
exampleMap.insert(std::make_pair("you", 0));
int expected = 0;
for (std::map<std::string, int>::const_iterator it = exampleMap.begin();
it != exampleMap.end();
++it) {
if (it->second == expected)
std::cout << it->first << std::endl;
}
}
Compilation and execution :
pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra m.cc
pi@raspberrypi:/tmp $ ./a.out
hello
you
pi@raspberrypi:/tmp $