I have a C++ code where I am instantiating an unordered_map and then printing it's values using cout. This works fine. But, when I try to run this in gdb and print the values of the unordered_map, this gives me error. Below, is the code snippet:
std::unordered_map<std::string,int> mymap = {
{ "Mars", 3000},
{ "Saturn", 60000},
{ "Jupiter", 70000 } };
std::cout<< mymap.at("Mars");
std::cout<< mymap["Mars"];
Both the cout statements above print the unordered_map value for key "Mars". However, when I use gdb and then try using below statements to print the value of mymap at key "Mars", I get errors.
(gdb) print mymap.at("Mars")
Cannot resolve method std::unordered_map<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int,
std::hash<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::equal_to<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
std::allocator<std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const, int> > >::at to any overloaded instance
(gdb) print mymap["Mars"]
Cannot resolve function operator[] to any overloaded instance
I do not get what is wrong when I use gdb.
I have tried using whatis mymap, in gdb, to see if mymap is present in current context and it gives that it is present. Also, I tried initializing an int variable and printing it in gdb and it prints it. I do not understand what is the problem with unordered_map.
I am using below statement to generate executable
gsrivas4@TitanX01:~/lcode1$ g++ -std=gnu++11 -O0 -g test1.cpp -o test1.out