自定义一个动态数组
2014-04-16 23:13
381 查看
/******************************动态数组****************************************/
#include <memory>
#include <new>
typedef unsigned char BYTE;
template<class Type>
class carray
{
public:
carray():m_nSize(0),pdata(NULL){}
~carray();
void add(Type newElement);
int getsize();
Type operator[](int nIndex) const;
Type& operator[](int nIndex);
private:
int m_nSize;
Type* pdata;
};
template<class Type>
carray<Type>::~carray()
{
if (pdata != NULL)
{
// for (;m_nSize--;pdata++)
// pdata->~Type();
delete[] (BYTE*)pdata;
}
}
/********************************************************************************/
/* 以下添加元素这个部分很重要 */
/********************************************************************************/
template<class Type>
void carray<Type>::add(Type newElement)
{
if (pdata == NULL)
{
pdata = (Type*) new BYTE[sizeof(Type)];
memset((void*)pdata,0,sizeof(Type));
::new((void*)pdata) Type;
}
else
{
Type* pNewData = (Type*) new BYTE[(m_nSize+1) * sizeof(Type)];
memcpy(pNewData,pdata,m_nSize*sizeof(Type));
memset((void*)&pNewData[m_nSize],0,sizeof(Type));
::new((void*)&pNewData[m_nSize]) Type;
delete[] (BYTE*)pdata;
pdata = pNewData;
}
pdata[m_nSize++] = newElement;
}
template<class Type>
int carray<Type>::getsize()
{
return m_nSize;
}
template<class Type>
Type carray<Type>::operator [](int nIndex) const
{
return pdata[nIndex];
}
template<class Type>
Type& carray<Type>::operator [](int nIndex)
{
return pdata[nIndex];
}
/*************************************************************************************/
#include <memory>
#include <new>
typedef unsigned char BYTE;
template<class Type>
class carray
{
public:
carray():m_nSize(0),pdata(NULL){}
~carray();
void add(Type newElement);
int getsize();
Type operator[](int nIndex) const;
Type& operator[](int nIndex);
private:
int m_nSize;
Type* pdata;
};
template<class Type>
carray<Type>::~carray()
{
if (pdata != NULL)
{
// for (;m_nSize--;pdata++)
// pdata->~Type();
delete[] (BYTE*)pdata;
}
}
/********************************************************************************/
/* 以下添加元素这个部分很重要 */
/********************************************************************************/
template<class Type>
void carray<Type>::add(Type newElement)
{
if (pdata == NULL)
{
pdata = (Type*) new BYTE[sizeof(Type)];
memset((void*)pdata,0,sizeof(Type));
::new((void*)pdata) Type;
}
else
{
Type* pNewData = (Type*) new BYTE[(m_nSize+1) * sizeof(Type)];
memcpy(pNewData,pdata,m_nSize*sizeof(Type));
memset((void*)&pNewData[m_nSize],0,sizeof(Type));
::new((void*)&pNewData[m_nSize]) Type;
delete[] (BYTE*)pdata;
pdata = pNewData;
}
pdata[m_nSize++] = newElement;
}
template<class Type>
int carray<Type>::getsize()
{
return m_nSize;
}
template<class Type>
Type carray<Type>::operator [](int nIndex) const
{
return pdata[nIndex];
}
template<class Type>
Type& carray<Type>::operator [](int nIndex)
{
return pdata[nIndex];
}
/*************************************************************************************/
相关文章推荐
- c++自定义一个动态数组vector
- 有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(动态开辟)
- Java数组反射实现动态的判断一个对象书否是数组,并且对数组进行拆包输出 。。
- 【C】FindString之华为软件训练营考试_动态创建一个两维数组
- Android的一个自定义的动态添加Dialog类
- 建立一个动态数组,赋值/显示并删除。
- 使用动态数组结构的一个好处
- 输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中最大值
- 已知一个数组 20 个元素(随机 1 到 100 之间包含 1 和 100),求大于平均数的元素个数,并动态生成一个新数组保存
- 【C语言】建立动态数组,输入五个学生的成绩,另外用一个函数检查有无60分以下的,输出不合格成绩
- 分别使用静态数组与动态数组建立一个区域 - 回复 "老A123" 的问题
- 有关数组 & 动态数组,输出数组中最长字符以及最短字符,若最长最短不止一个,都输出
- 将js里的一个数组里面的值动态地添加到一个下拉框中
- 输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值
- asp.net 自己封装数据库操作一个类中一个自定义方法Execute(),非常实用,省去了麻烦的中间过程,动态参数
- 输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值
- Java 从控制台读入一个动态字符串数组
- 一个动态数组的例子C++
- 使用动态数组结构的一个好处
- 将一个长度不定数组按照自定义标准进行排序