468,738 Members | 1,593 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

Dynamic multidimensional array using auto_ptr

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?
Jun 8 '06 #1
8 6143

Skay wrote:
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.)

Jun 8 '06 #2
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?
}
"Daniel T." <da******@earthlink.net> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
Write a basic matrix class using std::vector, then use the adaptor from
the gotw article. The FAQ shows how to write a 2 dimen besional matrix
class (though it doesn't use std::vector internally which is a real
shame.)

Jun 8 '06 #3

Skay wrote:
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.

Jun 8 '06 #4

Noah Roberts wrote:
Skay wrote:
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.


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?)

Jun 8 '06 #5

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


I see only shared_ptr and weak_ptr in tr1...which is viewable here btw:

http://www.open-std.org/jtc1/sc22/wg...2005/n1745.pdf

Jun 8 '06 #6

Skay wrote:
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.

Jun 8 '06 #7

Daniel T. wrote:
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?

Jun 8 '06 #8

Earl Purple wrote:
Daniel T. wrote:
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?


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

Jun 9 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gregory L. Hansen | last post: by
9 posts views Thread by Charles Banas | last post: by
13 posts views Thread by xian_hong2046 | last post: by
9 posts views Thread by JoeC | last post: by
24 posts views Thread by VijaKhara | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.