Jump to content
  • Advertisement
Sign in to follow this  
lord4n

2D Arrays in C++

This topic is 4371 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

I know that its better to use Vector than Array in c++, but how should i use Vector to create 2D Array || 3D Array ?

Share this post


Link to post
Share on other sites
Advertisement
std::vector< std::vector<int> >
fro a 2d array

this creates a vector of vectors so for 3d u just say

std::vector< std::vector< std::vector<int> > >
and so on for more dimensions.

hope that helps

Share this post


Link to post
Share on other sites
The difficulty with vectors (and dynamic C "arrays") is that they are potentially ragged. The tool of choice for representing matrices is not a vector of vectors (its semantics are no better than those of C arrays), but boost::multi_array.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
The difficulty with vectors (and dynamic C "arrays") is that they are potentially ragged. The tool of choice for representing matrices is not a vector of vectors (its semantics are no better than those of C arrays), but boost::multi_array.


I have heard about those boost libraries but they're not part of the C++
Syntax so i should download them anyway?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Many of the boost libraries have become part of the c++ technical release, they are made by C++ experts to create well thought extra libraries and most PC based developers consider boost as good as a official C++ library.

That being said, they can be bloated for console development (like PS2, XBox) etc, and generally in those circumstances you want to do testing to see which parts of boost you can and can't use, but this probably doesn't effect many people here but worth a mention (we at work use current gen and next gen consoles and tend to use boost but only specific libraries that have turned up alright performance in testing)

Share this post


Link to post
Share on other sites
okay, i download the boost libraries and i was trying to use it, but im have some problems..

#include <cassert>
#include "boost/multi_array.hpp"

using namespace std;

int main(int argc, char** argv)
{
typedef boost::multi_array<double, 3> array_type;

array_type A(boost::extents[3][4][2]);
boost::array<array_type::index,3> idx = {{0,0,0}};

A(idx) = 3.14;
assert(A(idx) == 3.14);

return 0;
}




it should work good i only copied the code from:
http://www.boost.org/libs/multi_array/doc/user.html#sec_access

it works ok, but im getting those weird errors:
http://rafb.net/paste/results/tAJjry16.html ().

how can i get rid of them ?

Share this post


Link to post
Share on other sites
You are getting warnings about uses of the boost library. You can disable them by wrapping their inclusion with #pragma pop, push and warning preprocessor statements. See the documentation of your compiler about these statements and how to use them.

Share this post


Link to post
Share on other sites
Quote:
From msdn:
#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
// Some code
#pragma warning( pop )


If you replace // some code by the inclusion of the boost headers, the warnings will be suppressed for these headers alone.

Share this post


Link to post
Share on other sites
Thanks.
you helped me a lot, where can i read more about those
boost libraries and learn about them ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!