Sign in to follow this  
Geno the Great

How do I create a multidimensional Vector?

Recommended Posts

Geno the Great    180
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[i][j] = something; } } (Too lazy to write the C++ code)

Share this post


Link to post
Share on other sites
Washu    7829
Just goes to show Oluseyi, you can lead a horse to water...


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

Share this post


Link to post
Share on other sites
cgoat    132
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this