您的位置:首页 > 其它

一个动态数组的实现.

2007-03-30 11:59 260 查看
在项目过程中,有时候需要一些能索引的,可以动态插入的,无长度限制的数据结构.
使用STL是可以很方便,但是在mobile上,不想引入STL. 就自己简单地实现了一个这样的结构.
采用模版类的方式来设计.用起来基本还可以.

模版的实现
template<class T>
class CDynArray
template<class T>
class CDynArray
class CLinkMan
{
public:
int nIndex; //索引
int nParIndex; //父索引
int nType; //类型 1为部门,2为人
int nDeptIndex; //所属部门索引

TCHAR tcValue[128]; //值
TCHAR tcValueIndex[128]; //值的索引

CLinkMan()
{
nIndex = -1;
nParIndex = -1;
nType = -1;
nDeptIndex = -1;

tcValue[0] = NULL;
tcValueIndex[0] = NULL;
}

CLinkMan(const CLinkMan& temp)
{
this->nIndex = temp.nIndex;
this->nParIndex = temp.nParIndex;
this->nType = temp.nType;
this->nDeptIndex = temp.nDeptIndex;

wsprintf(this->tcValue,_T("%s"),temp.tcValue);
wsprintf(this->tcValueIndex,_T("%s"),temp.tcValueIndex);
}

CLinkMan& operator = (CLinkMan& temp)
{
this->nIndex = temp.nIndex;
this->nParIndex = temp.nParIndex;
this->nType = temp.nType;
this->nDeptIndex = temp.nDeptIndex;

wsprintf(this->tcValue,_T("%s"),temp.tcValue);
wsprintf(this->tcValueIndex,_T("%s"),temp.tcValueIndex);

return *this;
}

void Reset()
{
nIndex = -1;
nParIndex = -1;
nType = -1;
nDeptIndex = -1;

tcValue[0] = NULL;
tcValueIndex[0] = NULL;
}

protected:
private:

};

最后的一些思考,在模版的设计里面,对于内存的申请,我是要多少,申请多少,应该可以考虑每次申请一批,慢慢用.
不过在结构不太大的情况下,应该没什么问题的.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐