LLDB:如何检查unordered_map

Ste*_* Lu 7 c++ xcode lldb c++11

大多数其他STL容器打印很好,但是unordered_map很乱.

operator <<用于打印,但这不是关于打印,这是关于我崩溃的时候,我想从LLDB提示打印出我的哈希.

我不能称之为是call cout << var因为这不起作用.

除了例如链接自己使用的模板函数之外,没有解决方案cout <<吗?那甚至会起作用吗?(我正在尝试,但它不起作用,因为我必须提前知道模板参数类型将为它生成和链接代码的内容)

tro*_*foe 0

您应该能够检查unordered_map对象本身,而不需要调用它的方法。

例如,采用这个简单的程序:

#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

int main() {
    unordered_map<int, string> map;
    map[0] = "mary";
    map[1] = "had";
    map[2] = "a";
    map[3] = "little";
    map[4] = "lamb";

    return 0;
}

$ clang++ -std=c++11 -stdlib=libc++ -g unmap.cpp -o unmap
$ lldb unmap
Current executable set to 'unmap' (x86_64).
(lldb) break set --name main
Run Code Online (Sandbox Code Playgroud)

lldb为简洁起见未显示输出

(lldb) proc launch
Run Code Online (Sandbox Code Playgroud)

n输入 5 次 Untilreturn 0;语句

(lldb)
Process 18063 stopped
* thread #1: tid = 0x1c03, 0x0000000100000aea unmap`main + 1082 at unmap.cpp:15, stop reason = step over
    frame #0: 0x0000000100000aea unmap`main + 1082 at unmap.cpp:15
   12       map[3] = "little";
   13       map[4] = "lamb";
   14
-> 15       return 0;
   16   }
   17
Run Code Online (Sandbox Code Playgroud)

然后使用以下命令检查对象p

(lldb) p map[0]
(std::__1::unordered_map<int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::hash<int>, std::__1::equal_to<int>, std::__1::allocator<std::__1::pair<const int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::mapped_type) $2 = "mary"
(lldb) p map[1]
(std::__1::unordered_map<int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::hash<int>, std::__1::equal_to<int>, std::__1::allocator<std::__1::pair<const int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::mapped_type) $3 = "had"
(lldb) quit
Run Code Online (Sandbox Code Playgroud)

您使用的版本lldb会有所不同,因为它一直在改进:

$ lldb -version
LLDB-179.5
Run Code Online (Sandbox Code Playgroud)

(即Xcode 5 DP 6命令行包自带的)