Mike Dooley
Mike Dooley

Reputation: 976

How to use a "vector of vector"?

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

Upvotes: 1

Views: 1221

Answers (4)

LonliLokli
LonliLokli

Reputation: 1315

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.

Upvotes: 1

Ben Collins
Ben Collins

Reputation: 20686

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);

Upvotes: 3

Edward Strange
Edward Strange

Reputation: 40877

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

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

Upvotes: 1

Kyra
Kyra

Reputation: 5407

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

Upvotes: 1

Related Questions