Help with Array template

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

    Help with Array template

    Hi,
    I am trying to write a template for an array which I can use to
    create multi-dimensional matrices as

    // A 10x10 matrix with each element initialized to 0
    Array< Array<int> > Matrix (10,0);

    // A 5x5x5 3-dimensional matrix with each element initialized to 1
    Array< Array <Array<int> > > Matrix3D (5,1);

    // A 15x15x15x15 4-dimensional matrix with each element initialized to
    10
    Array< Array< Array< Array<int> > > > Matrix4D(15,10) ;

    The code for the array class is as follows:

    template < class T> class Array
    {
    private:
    T* _data;
    public:
    Array(int Size, T Initializer)
    {
    int i;
    _data= new T[Size];
    for(i=0;i<Size; i++)
    {
    _data[i]=Initializer;
    /*************** *************** *************** *******
    /***** OR SHOULD I USE
    _data[i]=T(Initializer) ;
    /*************** *************** *************** *******/
    }
    }
    };

    Now if I try and declare a matrix as

    Array< Array<int> > Matrix (10, 10 );

    the compiler cribs as it expects an initializer of type Array<int> for
    the Initializer when it does
    _data[i]=T(Initializer) ;

    Can someone please help me out ? how do I write a template for an
    Array such that I can easily extend it to multidimensions
    thanks a lot in advance.

    Regards,
    Madhu
  • Buster

    #2
    Re: Help with Array template

    [color=blue]
    > Now if I try and declare a matrix as
    >
    > Array< Array<int> > Matrix (10, 10 );
    >
    > the compiler cribs as it expects an initializer of type Array<int> for
    > the Initializer when it does
    > _data[i]=T(Initializer) ;[/color]


    Try

    Array <Array <int> > Matrix (10, Array <int> (10));

    Regards,
    Buster.


    Comment

    • Julián Albo

      #3
      Re: Help with Array template

      hrmadhu escribió:
      [color=blue]
      > I am trying to write a template for an array which I can use to
      > create multi-dimensional matrices as[/color]

      I modified your code giving this:

      #include <memory>

      template <class T> class Array
      {
      private:
      T * _data;
      int s;
      public:
      Array(int Size, const T & Initializer)
      {
      s= Size;
      int i;
      _data= std::allocator <T> ().allocate (Size);
      for(i=0;i<Size; i++)
      {
      new (& _data [i] ) T (Initializer);
      }
      }
      Array (const Array & a)
      {
      _data= std::allocator <T> ().allocate (a.s);
      for (int i= 0; i < a.s; ++i)
      {
      new (& _data [i] ) T (a._data [i] );
      }
      }
      ~Array ()
      {
      for (int i= 0; i < s; ++i)
      {
      _data [i].~T ();
      }
      std::allocator <T> ().deallocate (_data, s);
      }
      };

      int main ()
      {
      // 10x10 matrix with values initialized to 1.
      Array <Array <int> >
      Matrix2D (10, Array <int> (10, 1) );
      // 10x10x10 matrix with values initialized to 1.
      Array <Array <Array <int> > >
      Matrix3D (10, Array <Array <int> > (10, Array <int> (10, 1) ) );
      }

      Warning! I don't tested it more than compiling and executing the result.

      You probably need to add an assignment operator to Array, or to forbid
      the assignment.

      Regards.

      Comment

      Working...