Jump to content
  • Advertisement


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


dynamic arrays with stl

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

If you intended to correct an error in the post then please contact us.

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 this post

Link to post
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:


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)

Hope that helps .

Share this post

Link to post
Share on other sites
The std::vector isn''t really suitable for 2d arrays.

Use boost.multi_array. You can wrap it around a 1d std::vector if you want to use that for memory management. It''ll be more efficient and easier to use correctly.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!