C++数组类模板
* 作为数组类模板,肯定没有vector做得好,可是普通的数组有1个优点就是能直接操作内存。vector在这方面就不是非常方便了。 网上尽管也有数组类模板。多维的设计基本上都不是非常好。我这个类模板多维的设计借鉴了vector,如2维数组vector<vector<int>> vvArr;下附源代码*/ #pragma once enum E_POSIITION
{
begin_position = 0,
end_position
}; // CBaseArray作为一个最原始基类,不同意构造对象
template <class T>
class CBaseArray
{
protected:
CBaseArray():m_pTMemory(NULL),m_uMemSize(0)
{ } virtual ~CBaseArray()
{ } public:
// member method
const T* GetMemory(UINT _uIdx = 0) const
{
ASSERT(_uIdx < m_uMemSize); return m_pTMemory+_uIdx;
}
T* GetMemory(UINT _uIdx = 0)
{
ASSERT(_uIdx < m_uMemSize); return m_pTMemory+_uIdx;
} const UINT GetSize() const
{
return m_uMemSize;
}
UINT GetSize()
{
return m_uMemSize;
} void MemCopy(UINT _uPos, const T* _pBuff, UINT _uSize)
{
ASSERT(NULL != _pBuff);
ASSERT(_uSize+_uPos <= m_uMemSize);
memcpy(GetMemory(_uPos), _pBuff, _uSize*sizeof(T));
} void ResetValue(const T tValue)
{
for (UINT uIdx = 0; uIdx < m_uMemSize; uIdx++)
{
MemCopy(uIdx, &tValue, 1);
}
} void ResetRandMemory()
{
srand(GetTickCount());
for (UINT uIdx = 0; uIdx < m_uMemSize*sizeof(T); uIdx++)
{
*(PBYTE(m_pTMemory) + uIdx) = rand();
}
} protected:
BOOL MallocMem(UINT _uSize)
{
// 先申请内存保存原有的数据
T *tTemp = new T[m_uMemSize];
ASSERT(NULL != tTemp); memcpy(tTemp, m_pTMemory, m_uMemSize*sizeof(T)); // 释放原有内存
delete[] m_pTMemory;
m_pTMemory = NULL; // 又一次分配内存,并保存足够的原有的数据
m_pTMemory = new T[_uSize];
ASSERT(NULL != m_pTMemory);
if (NULL == m_pTMemory)
{
return FALSE;
} UINT uMinSize = min(_uSize, m_uMemSize);
memcpy(m_pTMemory, tTemp, uMinSize*sizeof(T));
ZeroMemory(m_pTMemory, uMinSize*sizeof(T)); m_uMemSize = _uSize;
delete[] tTemp;
tTemp = NULL; return TRUE;
} T *m_pTMemory;
UINT m_uMemSize;
}; // 1维数组模板类
template <class T>
class CMyArray : public CBaseArray<T>
{
public:
CMyArray()
{ } CMyArray(UINT _uSize, const T& _tValue)
{
SetSize(_uSize);
ResetValue(_tValue);
} CMyArray(const CMyArray& _other)
{
if (m_uMemSize > 0)
{
delete[] m_pTMemory;
m_pTMemory = NULL;
} m_pTMemory = new T[_other.m_uMemSize];
ASSERT(NULL != m_pTMemory); m_uMemSize = _other.m_uMemSize;
memcpy(m_pTMemory, _other.m_pTMemory, _other.m_uMemSize*sizeof(T));
} ~CMyArray()
{
if ((m_uMemSize > 0) && (NULL != m_pTMemory))
{
delete[] m_pTMemory;
m_pTMemory = NULL;
}
} // member method
BOOL SetSize(UINT _uSize)
{
return MallocMem(_uSize);
}
BOOL SetSize(UINT _uSize, const T& _tValue)
{
if (!SetSize(_uSize))
{
return FALSE;
} ResetValue(_tValue); return TRUE;
} void ReleaseArray()
{
if (m_uMemSize > 0)
{
delete[] m_pTMemory;
m_pTMemory = NULL;
m_uMemSize = 0;
}
} // 重载两种类型的[]操作符
const T& operator[](UINT _uIdx) const
{
ASSERT(_uIdx < m_uMemSize); return m_pTMemory[_uIdx];
}
T& operator[](UINT _uIdx)
{
ASSERT(_uIdx < m_uMemSize); return m_pTMemory[_uIdx];
} CMyArray& operator=(const CMyArray& _other)
{
ReleaseArray(); m_pTMemory = new T[_other.m_uMemSize];
ASSERT(NULL != m_pTMemory); m_uMemSize = _other.m_uMemSize;
memcpy(m_pTMemory, _other.m_pTMemory, _other.m_uMemSize*sizeof(T)); return *this;
} BOOL operator==(const CMyArray& _other)
{
if (m_uMemSize == _other.GetSize())
{
if (0 == memcmp(GetMemory(0), _other.GetMemory(0), m_uMemSize*sizeof(T)))
{
return TRUE;
}
} return FALSE;
} void Insert(E_POSIITION _ePostion, const CMyArray& _other)
{
// 建立暂时变量先保存当前对象
CMyArray myTemp(*this); // 释放当前对象的内容
ReleaseArray(); // 为当前对象申请新的内存以存放合并后的内容
SetSize(myTemp.GetSize() + _other.GetSize()); if (begin_position == _ePostion)
{
MemCopy(0, _other.GetMemory(0), _other.GetSize());
MemCopy(_other.GetSize(), myTemp.GetMemory(0), myTemp.GetSize());
}
else if (end_position == _ePostion)
{
MemCopy(0, myTemp.GetMemory(0), myTemp.GetSize());
MemCopy(myTemp.GetSize(), _other.GetMemory(0), _other.GetSize());
}
} protected:
private:
}; // 2维数组模板类
template<class T>
class CMy2DArray
{
public:
CMy2DArray()
{ } CMy2DArray(UINT _uX, UINT _uY)
{
SetSize(_uX, _uY);
} CMy2DArray(UINT _uX, UINT _uY, const T& _tValue)
{
SetSize(_uX, _uY);
ResetValue(_tValue);
} CMy2DArray(const CMy2DArray& _other)
{
SetSize(_other.m_uX, _other.m_uY);
for (UINT uRow = 0; uRow < _other.m_uX; uRow++)
{
m_tArray[uRow] = _other.m_tArray[uRow];
}
} ~CMy2DArray()
{ } // member method
// 设置二维数组的大小
BOOL SetSize(UINT _uX, UINT _uY)
{
m_uX = _uX;
m_uY = _uY; if (!m_tArray.SetSize(_uX))
{
return FALSE;
} for (UINT uIdx = 0; uIdx < _uX; uIdx++)
{
if (!m_tArray[uIdx].SetSize(_uY))
{
return FALSE;
}
} return TRUE;
} void ResetValue(const T tValue)
{
for (UINT uRow = 0; uRow < m_uX; uRow++)
{
m_tArray[uRow].ResetValue(tValue);
}
} void ResetRandMemory()
{
for (UINT uRow = 0; uRow < m_uX; uRow++)
{
m_tArray[uRow].ResetRandMemory();
}
} UINT GetXSize()
{
return m_uX;
} UINT GetYSize()
{
return m_uY;
} T* GetMemory(UINT _uX, UINT _uY)
{
ASSERT(_uX < m_uX);
ASSERT(_uY < m_uY); return m_tArray[_uX].GetMemory(_uY);
} const T& GetValue(UINT _uX, UINT _uY)
{
ASSERT(_uX < m_uX);
ASSERT(_uY < m_uY); return m_tArray[_uX][_uY];
} // 重载两个[]运算符,前一个为常量对象使用,后一个为非常量对象使用
const CMyArray<T>& operator[](UINT _uIdx) const
{
ASSERT(_uIdx < m_uX); return m_tArray[_uIdx];
}
CMyArray<T>& operator[](UINT _uIdx)
{
ASSERT(_uIdx < m_uX); return m_tArray[_uIdx];
} // 重载强制类型转换符
operator CMyArray<CMyArray<T>>&()
{
return m_tArray;
} // 重载()操作符
CMyArray<CMyArray<T>>& operator ()()
{
return m_tArray;
} // 重载=运算符
CMy2DArray& operator=(const CMy2DArray& _other)
{
ReleaseArray(); SetSize(_other.m_uX, _other.m_uY);
for (UINT uRow = 0; uRow < _other.m_uX; uRow++)
{
m_tArray[uRow] = _other.m_tArray[uRow];
} return *this;
} protected:
void ReleaseArray()
{
m_tArray.ReleaseArray();
} private:
UINT m_uX;
UINT m_uY;
CMyArray<CMyArray<T>> m_tArray;
}; // 3维数组模板类
template<class T>
class CMy3DArray
{
public:
CMy3DArray() : m_uX(0), m_uY(0), m_uZ(0)
{ } CMy3DArray(UINT _uX, UINT _uY, UINT _uZ)
{
SetSize(_uX, _uY, _uZ);
} CMy3DArray(UINT _uX, UINT _uY, UINT _uZ, const T& _tValue)
{
SetSize(_uX, _uY, _uZ);
ResetValue(_tValue);
} CMy3DArray(const CMy3DArray& _other)
{
SetSize(_other.m_uX, _other.m_uY, _other.m_uZ);
for (UINT uRow = 0; uRow < _other.m_uX; uRow++)
{
m_t3DArr[uRow] = _other.m_t3DArr[uRow];
}
} ~CMy3DArray()
{ } // member method
BOOL SetSize(UINT _uX, UINT _uY, UINT _uZ)
{
m_uX = _uX;
m_uY = _uY;
m_uZ = _uZ; if (!m_t3DArr.SetSize(_uX))
{
return FALSE;
}
for (UINT uIdx = 0; uIdx < _uX; uIdx++)
{
if (!m_t3DArr[uIdx].SetSize(_uY, _uZ))
{
return FALSE;
}
} return TRUE;
} void ResetValue(const T tValue)
{
for (UINT uRow = 0; uRow < m_uX; uRow++)
{
m_t3DArr[uRow].ResetValue(tValue);
}
} void ResetRandMemory()
{
for (UINT uRow = 0; uRow < m_uX; uRow++)
{
m_t3DArr[uRow].ResetRandMemory();
}
} T* GetMemory(UINT _uX, UINT _uY, UINT _uZ)
{
ASSERT(_uX < m_uX);
ASSERT(_uY < m_uY);
ASSERT(_uZ < m_uZ); return m_t3DArr[_uX].GetMemory(_uY, _uZ);
} // 重载两个[]运算符,前一个为常量对象使用,后一个为非常量对象使用
const CMy2DArray<T>& operator[](UINT _uIdx) const
{
ASSERT(_uIdx < m_uX); return m_t3DArr[_uIdx];
}
CMy2DArray<T>& operator[](UINT _uIdx)
{
ASSERT(_uIdx < m_uX); return m_t3DArr[_uIdx];
} // 重载=运算符
CMy2DArray<T>& operator=(const CMy3DArray<T>& _other)
{
ReleaseArray(); SetSize(_other.m_uX, _other.m_uY, _other.m_uZ);
for (UINT uRow = 0; uRow < _other.m_uX; uRow++)
{
m_t3DArr[uRow] = _other.m_t3DArr[uRow];
} return *this;
} void ReleaseArray()
{
for (UINT uIdx = 0; uIdx < m_uX; uIdx++)
{
m_t3DArr[uIdx].ReleaseArray();
}
} UINT GetXSize()
{
return m_uX;
} UINT GetYSize()
{
return m_uY;
} UINT GetZSize()
{
return m_uZ;
} private:
UINT m_uX;
UINT m_uY;
UINT m_uZ;
CMyArray<CMy2DArray<T>> m_t3DArr;
};
C++数组类模板的更多相关文章
- 网易云课堂_C++程序设计入门(下)_第10单元:月映千江未减明 – 模板_第10单元 - 单元作业:OJ编程 - 创建数组类模板
第10单元 - 单元作业:OJ编程 - 创建数组类模板 查看帮助 返回 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...
- C++走向远洋——64(项目三、数组类模板)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- C++解析(27):数组、智能指针与单例类模板
0.目录 1.数组类模板 1.1 类模板高效率求和 1.2 数组类模板 1.3 堆数组类模板 2.智能指针类模板 2.1 使用智能指针 2.2 智能指针类模板 3.单例类模板 3.1 实现单例模式 3 ...
- C++——模板、数组类
1.函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 声明方法:template<typename 标识符> 函数声明 求绝对值的模板 #in ...
- C++中的数据类模板
1,预备知识: 1,模板参数可以是数值型参数(非类型参数): 1,代码示例: template <typename T, int N> void func() { T a[N]; // 使 ...
- 类模板、Stack的类模板实现(自定义链栈方式,自定义数组方式)
一.类模板 类模板:将类定义中的数据类型参数化 类模板实际上是函数模板的推广,可以用相同的类模板来组建任意类型的对象集合 (一).类模板的定义 template <类型形参表> clas ...
- YTU 2642: 填空题:类模板---求数组的最大值
2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec 内存限制: 128 MB 提交: 646 解决: 446 题目描述 类模板---求数组的最大值 找出一个数组中的元 ...
- C++基础 (9) 第九天 编译器对模板类的二次编译 类模板 自定义数组类
1 昨日回顾 2 编译器对于模板的二次编译 写一个模板函数 然后进行调用 g++ template.cpp -o template // 汇编 g++ -S template.cpp –o templ ...
- 使用类模板的C++线性表实现(数组方式)
main.h #ifndef _MAIN_H_ #define _MAIN_H_ #include <iostream> #include <exception> #inclu ...
随机推荐
- Discuze修改用户名长度限制
第一步,在网站 uc_client\model 目录下的 user.php文件中,找到如下代码: ? 1 if($len > 15 || $len < 3 || preg_match(&q ...
- SQLServer2008 字符串函数一览表
/* 字符串函数 (PS.索引都从1开始计算)*/ /* 指定字符(或字符串)A.字符串B.起始索引.获得A在B中的索引值.*/select Charindex('d','abcdefg',0) -- ...
- 【MySQL】源码安装
操作系统:Red Hat Enterprise Linux Server release 6.5 Mysql安装包:mysql-5.6.4-m7.tar.zip,下载地址:http://pan.bai ...
- dell inspiron 15 3000 装XP win7 等GHOST系统方法
dell inspiron 装XP win7 等GHOST系统方法 . 开机按F2,进入BIOS .在 BIOS 的Boot菜单下,将Secure Boot 改为 Disabled . 将Boot L ...
- jenkins如何实现重新发布历史构建记录里的版本
Jenkins以前打包都会将打出的拷贝放到历史版本里放到Daily_Result里,昨天不只是误操作还是系统问题,误将一个历史版本的包删掉了,而且这个包是之前比较稳定的一个版本,需要重新给客户发,所以 ...
- MAMP PRO mysql无法启动
mac上可能勾选了软件自动更新,然后MAMP PRO 升级了. 升级了之后,mysql启动就出问题了,看报错日志: InnoDB: The error means the system cannot ...
- Nginx.conf介绍
在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data; #启动进程,通常设置成和cpu的数量相等worker_process ...
- 团体程序设计天梯赛-练习集-L1-046. 整除光棍
L1-046. 整除光棍 这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1.11.111.1111等.传说任何一个光棍都能被一个不以5结尾的奇数整除.比如,111111就可以被 ...
- 实现Modbus ASCII多主站应用
1.更新设计关于原来的协议栈在Modbus ASCII主站应用时所存在的局限性与Modbus RTU也是一样的,所以我们不分析它的不足,只讨论更新设计.我们将主站及其所访问的从站定义为通用的对象,而当 ...
- vmware Horizon 7 与远程桌面(mstsc)兼容性问题解决办法
关于Horizon 7 Agent与远程桌面(mstsc)兼容性问题解决办法 在Horizon 7环境中,在桌面模板安装了Horizon Agent后,就无法直接通过微软的远程桌面(mstsc)工具连 ...