两维的动态数组的C++封装
标签:数据结构
问题描述:
1.包装一个可扩展的动态的两维数组,支持泛型;
2.把存取的细节都包装起来,留一些简单的接口供外部使用;
3.稍作扩展可代替STL vector 使用;
程序代码:
#ifndef _TWODIM_ARRAY_H_
#define _TWODIM_ARRAY_H_
#include
#include
/*
* encapsulate two dimension array
*
*/
template
class TwoDimArray
{
public:
TwoDimArray():m_storage(0), m_row(ROW), m_col(COL),
m_curRow(0), m_totalCount(0)
{
}
/*
*Copy constructor
*
*/
template
TwoDimArray( const TwoDimArray& rhs ):m_storage(0), m_row(ROW), m_col(COL),
m_curRow(0), m_totalCount(0)
{
for( size_t i = 0; i
TwoDimArray& operator = ( const TwoDimArray& rhs )
{
if( this != &rhs )
{
Clear();
for( size_t i = 0; i 0 );
return (*this)[idx - 1];
}
/*
*
*
*/
T& Next( size_t idx )
{
assert( idx 0 );
return (*this)[idx - 1];
}
/*
*
*
*/
const T& Next( size_t idx ) const
{
assert( idx m_curRow )
{
m_curRow++;
m_storage[m_curRow] = new T[m_col];
memset( m_storage[m_curRow], 0x00, sizeof(T) * m_col );
return &m_storage[m_curRow][0];
}
else
{
return &m_storage[curRow][m_totalCount % m_col];
}
}
private:
T** m_storage;
size_t m_col;
size_t m_row;
size_t m_curRow;
size_t m_totalCount;
};
/*
*Test interface
*
*/
void TestTwoDimArray()
{
TwoDimArray dimArr;
int len = 6 * 6;
for( int i = 0; i newdimArr(dimArr);
assert( newdimArr.Size() == dimArr.Size() );
for( int i = 0; i
compile and run in visual studio 2005
两维的动态数组的C++封装,搜素材,soscw.com
两维的动态数组的C++封装
标签:数据结构
原文地址:http://blog.csdn.net/manthink2005/article/details/24812415
评论