一个动态数组的实现.
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:
};
最后的一些思考,在模版的设计里面,对于内存的申请,我是要多少,申请多少,应该可以考虑每次申请一批,慢慢用.
不过在结构不太大的情况下,应该没什么问题的.
使用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:
};
最后的一些思考,在模版的设计里面,对于内存的申请,我是要多少,申请多少,应该可以考虑每次申请一批,慢慢用.
不过在结构不太大的情况下,应该没什么问题的.
相关文章推荐
- Java数组反射实现动态的判断一个对象书否是数组,并且对数组进行拆包输出 。。
- Java数组反射实现动态的判断一个对象书否是数组,并且对数组进行拆包输出 。。
- Java数组反射实现动态的判断一个对象书否是数组,并且对数组进行拆包输出 。。
- 一个动态数组实现的简单栈
- 定义一个数组类(Array),要求动态分配大小,并在此基础上实现以下的两个子类:?反转类?排序类
- 一个动态多维数组的实现
- java数组反射实现动态的判断一个对象是否是数组并且对数组进行拆包输出
- c语言实现一个简单的通用动态数组
- 第三百五十节,Python分布式爬虫打造搜索引擎Scrapy精讲—selenium模块是一个python操作浏览器软件的一个模块,可以实现js动态网页请求
- 有一个数组,每次从中间随机取一个,然后放回去,当所有的元素都被取过,返回总共的取的次数。写一个函数实现。复杂度是什么。
- ~一个数组实现两个栈~
- C++ 以传入vector的方式实现返回一个数组
- 实现函数 makeClosures,调用之后满足如下条件: 1、返回一个函数数组 result,长度与 arr 相同 2、运行 result 中第 i 个函数,即 result[i](),结果与 fn
- 解决vc++DEBUG ERROR问题前几天师弟调试程序的时候出现了这样一个错误,出错的位置是在delete [] 一个动态分配的数组时出现的。 经过调查发现错误是因为他之前在给数组赋值的时候越界了
- 字符串替换空格:请实现一个函数,把字符数组中的每个空格替换成“%20”
- 创建一个数组,实现数组元素的初始化,逆置,清空
- 数据结构学习---堆栈的动态数组实现及链表实现
- 点击HeadView 实现对cell动态下拉一个TableView,实现对SectionHeader悬停进行设置
- JavaScript实现将数组中所有元素连接成一个字符串的方法
- Android--实现一个函数对多个CheckBox动态监