繁体   English   中英

如何查看GDB中的std:unordered_map成员

[英]how to view std:unordered_map member in GDB

当尝试使用 [] 访问 std::unordered_map 的成员时,出现错误:

尝试获取不在 memory 中的地址值。

有一个不错的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");
}

我在Archlinux中使用gdb 11.2

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

希望您找到打印 unordered_map 的正确方法。 请你在这个问题下告诉我们!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM