How do I create a multidimensional Vector?

Started by
5 comments, last by Washu 19 years, 6 months ago
Can someone show me how to create a vector with the dimensions ROW, COL (will end up as 15, 20) that can be accessable by myvector[row][col]? I need it to work like this: 1. The vector is declared in the object 2. In the constructor, the vector is given it's sizes. 3. Later, the vectors values are set using a loop (They'll be read from a file) that works like loop i { loop j { myvector[j] = something; } } (Too lazy to write the C++ code)
- - - - - - - - - - - - - - - -I am you and what I see is me
Advertisement
vector of vectors:
typedef vector< vector<Type> > Type2DArray;
That doesn't really help me in setting the size and such.
- - - - - - - - - - - - - - - -I am you and what I see is me
Just goes to show Oluseyi, you can lead a horse to water...

std::vector<std::vector<int> > myvec(15, std::vector<int>(20));

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

I believe the following will work:

Class definition:

std::vector< std::vector< int > > m_vector;

Initialization list:

m_vector(20, std::vector< int >::vector(30, 0))

That'll give you [20][30] sized, with all values initially set to 0.

Edit: Too late, what he ^ said. Mine is wrong anyway.
Damn this STL stuff, I'm completely new to it, so I don't understand half of it (Except string functions, I love them)

Is there any form of book on it?
- - - - - - - - - - - - - - - -I am you and what I see is me
Why yes! There is!
Books.VirtuallyOnline.Net

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement