5 Replies - 1513 Views - Last Post: 20 February 2011 - 03:14 PM Rate Topic: -----

#1 g_niro  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 26-January 11

how to print out element address of vector of vector

Posted 07 February 2011 - 08:38 AM

i have a vector of vector.
vector< vector<double> out_list.

here, inner vector contains only two elements. so my vector of vector stores values like, {{a1,b1},{a2,b2},{a3,b3},...}
i need to print out address of {a1,b1}, {a2,b2},...and so on. the addresses will be 0,1,2,3,..

I just tried with vector<int>, to print out its element address first. here is my code.
vector <int> n_list;
int main(){
    for (int i=0; i<11; i++){
        int x=3*i;
        n_list.push_back(x);
        }

vector <int>::iterator a;
for (a=n_list.begin();a!=n_list.end();a++){
    cout<<&(*a)<<" ";
    }
}





i got wrong output and it was
0x3e2500 0x3e2504 0x3e2508 0x3e250c 0x3e2510 0x3e2514 0x3e2518 0x3e251c 0x3e2520
0x3e2524 0x3e2528.

but, output should be like 0 1 2 3 4 ....
so, how can i print out element address of vectors?
then, i need to implement the same for vector of vector. hope your help.

Is This A Good Question/Topic? 0
  • +

Replies To: how to print out element address of vector of vector

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: how to print out element address of vector of vector

Posted 07 February 2011 - 08:44 AM

What you want is called index, not address.

But what exactly are you trying to do ?

This post has been edited by chinchang: 07 February 2011 - 08:47 AM

Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4068
  • View blog
  • Posts: 12,550
  • Joined: 25-December 09

Re: how to print out element address of vector of vector

Posted 07 February 2011 - 08:47 AM

I don't understand what you are trying to do, what you are doing now is printing out the address of each vector item. Are you trying to print out the contents of the vector? If you want the value held in position 3 of the vector then you could just use array notation cout << x[3] << " ".




Jim
Was This Post Helpful? 0
  • +
  • -

#4 g_niro  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 26-January 11

Re: how to print out element address of vector of vector

Posted 07 February 2011 - 10:30 AM

thank you for the information. so, i need 'index' of vector of vector. i.e. 0,1,2,3,4,5,…. But, I need to refer index not orderly.

i am using another map object. If I display the map line by line for more readability it’s as;
0 328 8 2 10 41 227 241 247 231 24 248 252 255 256 260
1 2 3 9 6
2 1 6 7 0 8 10 12 17 20 5 3
3 1 9 6 2 5 4
4 9 5 3
5 9 4 6 316 11 14 12 13 15 16 19 20 2 17 24 21 300 303 305 307 3
6 2 3 7 9 8 5 12 1
…. (0,1,2,3,4,… first values of each line are keys of map)

If I display the vector of vector, line by line it’s as,
24.2853 121.238
27.3207 115.146
24.3462 121.496
23.9153 300.713
24.5104 121.719
23.4989 300.099
83.5923 211.142
50.6657 119.183
... (this contains doubles)

So, I need to use 2 loops to incorporate these two data sets. Each integer in the map (either key or data item) refers to one index of vector of vector. What I wanted is, to access these inside vector according to the order given by the map.
I hope, it is now clear.

This post has been edited by g_niro: 07 February 2011 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: how to print out element address of vector of vector

Posted 07 February 2011 - 01:23 PM

Vectors are really just dynamic arrays ...
And they allow access using array index notation.
So ... can access like this:
vAry[0][1]
where the first 0 index specifies the index of the outer vector container
and the second 1 index specifies the index of the inner vector container.

BUT make sure every index you use is valid!

This post has been edited by David W: 07 February 2011 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#6 g_niro  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 26-January 11

Re: how to print out element address of vector of vector

Posted 20 February 2011 - 03:14 PM

thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1