c++debuggingstlvectorgdb

How do I print the elements of a C++ vector in GDB?


I want to examine the contents of a std::vector in GDB, how do I do it? Let's say it's a std::vector<int> for the sake of simplicity.


Solution

  • To view vector std::vector myVector contents, just type in GDB:

    (gdb) print myVector
    

    This will produce an output similar to:

    $1 = std::vector of length 3, capacity 4 = {10, 20, 30}
    

    To achieve above, you need to have gdb 7 (I tested it on gdb 7.01) and some python pretty-printer. Installation process of these is described on gdb wiki.

    What is more, after installing above, this works well with Eclipse C++ debugger GUI (and any other IDE using GDB, as I think).