Dynamic multidimensional array using auto_ptr

S

Skay

Someone challenged me to create a dynamic multidimensional array using
auto_ptr

http://www.gotw.ca/gotw/042.htm
Provides a good way to create a single dimensional array using an adapter.

However I am unable to come up with a way to create and access an array for
more than 1-dimension.
Any one up for the challenge?
 
D

Daniel T.

Skay said:
Someone challenged me to create a dynamic multidimensional array using
auto_ptr

http://www.gotw.ca/gotw/042.htm
Provides a good way to create a single dimensional array using an adapter.

However I am unable to come up with a way to create and access an array for
more than 1-dimension.
Any one up for the challenge?

Write a basic matrix class using std::vector, then use the adaptor from
the gotw article. The FAQ shows how to write a 2 dimensional matrix
class (though it doesn't use std::vector internally which is a real
shame.)
 
S

Skay

If this was for a production for an application using vector for a 2-d
matrix would be my choice too.
However this is essentially a theorotical question.

My current attempt:

int m = 3; int n = 4;
auto_ptr<ArrDelAdapter<auto_ptr<ArrDelAdapter<int> > > >
matrix( new ArrDelAdapter<auto_ptr<ArrDelAdapter<int> > >(new
auto_ptr<ArrDelAdapter<int> >[m]));

for (int i=0; i<m; i++)
{
auto_ptr<ArrDelAdapter<int> > row( new ArrDelAdapter<int>(new int[n]) );
*(matrix+i) = row; // Error: How to do this?
}
 
N

Noah Roberts

Skay said:
Someone challenged me to create a dynamic multidimensional array using
auto_ptr

http://www.gotw.ca/gotw/042.htm
Provides a good way to create a single dimensional array using an adapter.

However I am unable to come up with a way to create and access an array for
more than 1-dimension.
Any one up for the challenge?

It's a pointless endevor. It would be like totally ignoring a round
wheel and inventing a square one.
 
E

Earl Purple

Noah said:
It's a pointless endevor. It would be like totally ignoring a round
wheel and inventing a square one.

The other option, not mentioned there, is to run over to boost
libraries and grab their scoped_array. You can also use scoped_ptr if
you stick your own deleter on it that calls delete[].

Whilst vector is usually the solution, it isn't always, generally in
the case where you need highly efficient code (the profiler tells you
you do) and you need a large array of POD values and you don't want to
initialise them but instead to read into them.

Here boost::shared_array<char> or boost::scoped_array<char> (depending
on the requirements) may well be your best option.

(Is scoped_array / shared_array going to be in tr1 or do you need to
use scoped_ptr / shared_ptr with a custom deleter?)
 
D

Daniel T.

Skay said:
If this was for a production for an application using vector for a 2-d
matrix would be my choice too.
However this is essentially a theorotical question.

My current attempt:

int m = 3; int n = 4;
auto_ptr<ArrDelAdapter<auto_ptr<ArrDelAdapter<int> > > >
matrix( new ArrDelAdapter<auto_ptr<ArrDelAdapter<int> > >(new
auto_ptr<ArrDelAdapter<int> >[m]));

for (int i=0; i<m; i++)
{
auto_ptr<ArrDelAdapter<int> > row( new ArrDelAdapter<int>(new int[n]) );
*(matrix+i) = row; // Error: How to do this?
}

Maybe I didn't come across well. I mean you can use the Matrix class
*as your adapter*.

class Matrix {
public:
Matrix( int cols, int rows );
int at( int x, int y ) const;
int& at( int x, int y );
};

auto_ptr< Matrix > matrix( new Matrix( 3, 4 ) );
matrix->at( 2, 0 ) = 5;

The easest way to implement the Matrix class is with a vector.
 
E

Earl Purple

Daniel said:
Maybe I didn't come across well. I mean you can use the Matrix class
*as your adapter*.

class Matrix {
public:
Matrix( int cols, int rows );
int at( int x, int y ) const;
int& at( int x, int y );
};

auto_ptr< Matrix > matrix( new Matrix( 3, 4 ) );
matrix->at( 2, 0 ) = 5;

The easest way to implement the Matrix class is with a vector.

Easier:

Matrix<int> matrix( 3, 4 );
matrix.at( 2, 0 ) = 5;

Why use auto_ptr at all when you don't need a pointer?
 
D

Daniel T.

Earl said:
Easier:

Matrix<int> matrix( 3, 4 );
matrix.at( 2, 0 ) = 5;

Why use auto_ptr at all when you don't need a pointer?

I agree, but for some ungodly reason the OP wants to use an auto_ptr,
so whatever.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top