简体   繁体   English

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

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

When trying to access a member of std::unordered_map using [], I get an error:当尝试使用 [] 访问 std::unordered_map 的成员时,出现错误:

Attempt to take address of value not located in memory.尝试获取不在 memory 中的地址值。

There is a nice gdb-stl-views , except it does not support unordered_map.有一个不错的gdb-stl-views ,除了它不支持 unordered_map。

Is there a similarly nice way to retrieve by key a member of unordered_map?是否有类似的好方法可以通过键检索 unordered_map 的成员?

I think you are capable of viewing the member of std::unordered_map with an additional trivial step:我认为您可以通过额外的简单步骤查看std::unordered_map的成员:

This is my test code:这是我的测试代码:

#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");
}

And I am using gdb 11.2 in Archlinux :我在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

hope you find the right way to print the unordered_map.希望您找到打印 unordered_map 的正确方法。 May u please tell us under this question!请你在这个问题下告诉我们!

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

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