2D Arrays in C++

Started by
8 comments, last by ToohrVyk 17 years, 6 months ago
I know that its better to use Vector than Array in c++, but how should i use Vector to create 2D Array || 3D Array ?
True knowledge exists in knowing that you know nothing.
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
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.
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?
True knowledge exists in knowing that you know nothing.
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)
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 ?
True knowledge exists in knowing that you know nothing.
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.
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.
Thanks.
you helped me a lot, where can i read more about those
boost libraries and learn about them ?
True knowledge exists in knowing that you know nothing.
boost.org?

This topic is closed to new replies.

Advertisement