#### Archived

This topic is now archived and is closed to further replies.

# dynamic arrays with stl

This topic is 4951 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I''m trying to have a variable sized 2d array that is initialized at run-time. I did this with the new operator and it works fine, but I''d like to do it using the stl. I thought it would go like std::vector< std::vector< float > > test; to create a 2d array of floats that I can size as I want, but it seems not to be the way. I''ve looked elsewhere and haven''t figured it out. Any ideas? Thanks! James

##### Share on other sites
The syntax gets ugly for varying size multi-dimensional vectors.

To initialize it to a certain size, you do something like this:

std::vector<std::vector<int> > m(10, std::vector<int>(20));

Thats a 10x20 matrix

To add a row, you will need to add a vector to m, using something like:

m.push_back(std::vector<int>(20));

To add a column, you need to push back an int to each vector inside m.

for(std::vector<int>::size_type i = 0; i < m.size(); ++i)m[i].push_back(int());

Hope that helps .