Re: 2 dimensional array within function
"David Wilkinson" <no-reply@effisols.com> ha scritto nel messaggio
news:uk4ziZKaIHA.484@TK2MSFTNGP06.phx.gbl...
Both these problems would not occur if you used std::vector.
I agree with David Wi.
std::vector, and STL containers in general, seem to offer better
"composibility" than MFC containers.
Moreover, if the purpose is to store a 2D matrix like MxN, then a viable
solution could be also to use a single std::vector (or even valarray), sized
M*N (rowCount * coumnCount), and provide a method to address each element,
converting from (row, column) pair index to a single index to access the
array, using a formula like this:
index = col + row*columnCount
e.g.
template <typename T>
class Matrix
{
public:
explicit Matrix( int columnCount, int rowCount )
: m_data( columnCount * rowCount ), // set matrix size
m_columnCount( columnCount ),
m_rowCount( rowCount )
{}
const T & at( int row, int column ) const
{
ASSERT( row >= 0 && row < m_rowCount );
ASSERT( column >= 0 && column < m_columnCount );
// Access specific element in 1D array
// (convert from 2D (row, column) to 1D array index)
return m_data.at( column + row * m_columnCount );
}
...
private:
std::vector< T > m_data;
int m_columnCount;
int m_rowCount;
};
Giovanni