2D array of structures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michi

    #1

    2D array of structures

    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.
  • Victor Bazarov

    #2
    Re: 2D array of structures

    "michi" <michi@fossilgr oup.net> wrote...[color=blue]
    > 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.[/color]

    This is covered in the FAQ. http://www.parashift.com/c++-faq-lite/


    Comment

    • someone else

      #3
      Re: 2D array of structures

      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 >::operator[]( 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: kanze@lts.sel.a lcatel.de
      GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
      Conseils en informatique industrielle --

      "michi" <michi@fossilgr oup.net> wrote in message
      news:Xns95AF179 35411michifossi lgroupnet@195.3 4.132.65...[color=blue]
      > 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.[/color]



      Comment

      • Gianni Mariani

        #4
        Re: 2D array of structures

        michi wrote:[color=blue]
        > 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.[/color]

        see:

        Comment

        Working...