# 2D Arrays in C++

## 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 on other sites
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 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 on other sites
Quote:
 Original post by ToohrVykThe 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++

##### Share on other sites
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 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 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 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 on other sites
Thanks.
you helped me a lot, where can i read more about those
boost libraries and learn about them ?

boost.org?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628368
• Total Posts
2982293

• 10
• 9
• 13
• 24
• 11