Kev*_* S. 8 c++ gdb unordered-map
当尝试使用[]访问std :: unordered_map的成员时,出现错误:
尝试获取不在内存中的值的地址.
有一个很好的gdb-stl-views,除了它不支持unordered_map.
有没有一种同样好的方法来通过键检索unordered_map的成员?
std::unordered_map我认为您可以通过一个额外的简单步骤来查看成员:
这是我的测试代码:
#include <iostream>
#include <unordered_map>
std::string make_key(const char *input) { return input; }// The additional function to make sure you could construct the key of your map in gdb from primitive type
int main(int argc, char **argv) {
std::unordered_map<std::string, int> map = {
{"bar", 100}
};
std::cout << map.at("bar");
}
Run Code Online (Sandbox Code Playgroud)
我正在gdb 11.2使用Archlinux:
g++ -std=gnu++11 -O0 -g unordered_map_test.cpp -o unordered_map_test
gdb unordered_map_test
(gdb) p map
$1 = std::unordered_map with 1 element = {["bar"] = 100}
// Perhaps it's useless to print all key-value pairs in map if you have a large map.
// Then you could print value of specific key
(gdb) p map.at(make_key("bar"))
$2 = (std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, int> > >::mapped_type &) @0x55555556eed8: 100 // 100 is the value of `bar`
// If you think it's annoying that there is too much type information above, you could just print the value after you know the address of value.
(gdb) p *0x55555556eed8
$3 = 100
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2657 次 |
| 最近记录: |