How to print the elements of a C++ std::vector with GNU Debugger GDB ?

Consider the following code {test.cpp}:

#include<vector>
using namespace std;
int main (void)
{
vector<int> u(3,0);
u[0]=78;
u[2]=-53;
int a=1;
}

Let’s compile with debug flag -g

g++ -g test.cpp -o test

And Launch gdb with test executable:

gdb test
GNU gdb (GDB) 7.2-ubuntu

We add a breakpoint at line 8 and we launch test executable

(gdb) break 8
Breakpoint 1 at 0x4007a3: file test.cpp, line 8.
(gdb) run
Starting program: /GDB/test 

Breakpoint 1, main () at test.cpp:8
8	int a=1;

To print all elements use

print *(your_vector._M_impl._M_start)@your_vector_size

Here:

(gdb) print *(u._M_impl._M_start)@3
$8 = {78, 0, -53}

To print the two first element:

(gdb) print *(u._M_impl._M_start)@2
$7 = {78, 0}

Finally to print an element:

(gdb) print *(u._M_impl._M_start)
$2 = 78
(gdb) print *(u._M_impl._M_start+0)
$3 = 78
(gdb) print *(u._M_impl._M_start+1)
$4 = 0
(gdb) print *(u._M_impl._M_start+2)
$5 = -53