1

I already searched on the web for it but I didn't get satisfying results.

I want to create something like

vector< vector<int*> > test_vector;

How do i fill this vector of vector? How to access it's members? Maybe someone knows some nice tutorials on the web?

kind regards mikey

Mike Dooley
  • 936
  • 11
  • 26
  • 5
    I would surely not use int* as the content of your vector. If you're storing numbers, just use int (or double/float/...). The use of pointers makes you have to delete each of them when your vector gets destroyed. Otherwise, you'll get a memory leak. – rubenvb Jun 15 '10 at 17:59
  • See: http://stackoverflow.com/questions/823562/multi-dimensional-vector – Shog9 Jun 15 '10 at 18:06
  • I know your concern, but that code snippet above was only a demonstration. In effect I have to deal with pointers to objects. – Mike Dooley Jun 15 '10 at 21:33

4 Answers4

3

Just remember that each element of test_vector is of type vector<int*>. You would fill test_vector by filling each element vector.

You can access this just like any multi-dimensional array. See:

int *p = test_vector[0][0];

Or:

int *p = test_vector.at(0).at(0);
Ben Collins
  • 20,538
  • 18
  • 127
  • 187
  • Thanks! I reconsidered the problem and in order I wrote some test programms which helped me in my understanding! – Mike Dooley Jun 16 '10 at 17:29
1

A question similar to yours was posted at DreamInCode: http://www.dreamincode.net/forums/topic/37527-vector-of-vectors/

Kyra
  • 5,129
  • 5
  • 35
  • 55
1

PS If you want to use some kind of a matrix, I would prefer to use only one dimensional vector and map the access (because of performance).

For example Matrix M with m rows and n columns: you can map call

M[i][j] = x to M[i*n+j] = x.

deft_code
  • 57,255
  • 29
  • 141
  • 224
LonliLokli
  • 1,295
  • 4
  • 15
  • 24
1

You fill a vector of vectors by putting vectors in it.

You access its members the same way you would any other vector.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125