top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to print all the elements of vector STL in c++?

0 votes
516 views

give the code.

posted Feb 20, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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
answer Feb 22, 2016 by Shivaranjini
Similar Questions
+2 votes

Write a C/C++ program to print all possible strings of length k that can be formed from a set of n characters?

+1 vote
+4 votes

What are the advantages of Vector over an array in C++? give Exmaple.

...