2D array of structures

M

michi

Hello,

I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can
do one-dimensional array, but I don't know how to specify the size of array when I want to do it 2D. I want
to do it using 'new' operator.

Thanks, michi.
 
S

someone else

using a simple google groups search ((c++ 2d array new), i found this
i have tested it, and it works like a charm.

(...)
If you need a 2D
array with both (or the second) dimension variable, you will have to
do it by hand, either by using an array of pointers, and allocating
each of the sub-arrays separately, or by declaring and allocating a 1D
array, and doing the index calculation by hand. (I generally choose
the second solution for quick and dirty software, encapsulated in a
class, of course.)

So, in the above case, I might write something like the following:

int n = 4 ;
int m = 5 ;

int* a = new int[ m * n ] ;
a[ m * i + j ] ; // e.g.: a[ i ][ j ].

A bit messy, but once it's hidden in a class, it's quite presentable.
-----------------
#include <iostream.h>

template< class T >
class Array2D
{
public :
Array2D( int n , int m ) ;
~Array2D() ;
T* operator[]( int j ) ;
private :
int dim2 ;
T* a ;
} ;

template< class T >
Array2D< T >::Array2D( int n , int m )
: dim2( n )
, a( new T[ n * m ] )
{
}

template< class T >
Array2D< T >::~Array2D()
{
delete [] a ;
}

template< class T >
T*
Array2D< T >::eek:perator[]( int j )
{
return &a[ dim2 * j ] ;
}


int
main()
{
Array2D< int > a( 4 , 5 ) ;
for ( int i = 0 ; i < 4 ; i ++ )
{
for ( int j = 0 ; j < 5 ; j ++ )
{
a[ i ][ j ] = 10 * i + j ;
cout << a[ i ][ j ] << endl ;
}
} return 0 ;
}
--------------
Obviously, I've cheated with regards to the copy constructor and the
assignment operator. Either declare them private, or provide real
ones.

The only tricky part is the return value of operator[], and even that
isn't too tricky, if you remember the relationship between arrays and
pointers. (Operator[] is used in normal expressions, the one place
where arrays and pointers *are* sort of the same thing.) Basically,
although the class allocates the array as a 1D array, it considers it
logically as a 2D array, doing index calculation as necessary. As we
said, C++ doesn't have 2D arrays, but it has arrays of arrays. So
operator[] pretends that its internal array (pointed to by a) is an
array[] of array[dim2] of T, and calculates the address of the
array[dim2] selected by the index. In theory, it would return the
selected array (or a reference to it); in practice, an array appearing
in an expression converts automatically to the address of its first
element, so operator[] returns the address of the first element of the
sub-array. Conveniently, this is a valid type of the (built-in)
operator[], which handles the "second" dimension. (Operator[] could
just as easily return a reference to an array, but this would take
some casting in the operator[] itself, and there is such a long
tradition for doing it this way.)
--
James Kanze email: (e-mail address removed)
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --

michi said:
Hello,

I need to initialize a 2 dimensional square arrays of structures. The size
of array I get from the user. I can
do one-dimensional array, but I don't know how to specify the size of
array when I want to do it 2D. I want
to do it using 'new' operator.

Thanks, michi.
http://groups.google.com.au/[email protected]&rnum=1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top