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 ...
随机推荐
- C99C新增内容
继上一篇复合文字之后,今天我们继续谈一谈C99C的新特性. C99标准是继C89标准之后的第二个C语言官方标准,于1999年12月1日正式发布,其中对数据类型(增加了对_Bool),关键字(增加了in ...
- python3爬取豆瓣排名前250电影信息
#!/usr/bin/env python # -*- coding: utf-8 -*- # @File : doubanmovie.py # @Author: Anthony.waa # @Dat ...
- Bootstrap栅格系统&媒体查询
bootstrap中几乎所有元素的盒子模型为IE下的盒模型,通俗点说就是box-sizing设置成了:border-box. 栅格系统 媒体查询 媒体查询是非常别致的"有条件的 CSS ...
- CSS选择器优先级计算
优先级从高到低排列,浏览器优先满足前面的规则 1,!important优先级最高 2,内联样式 3,作者>读者>浏览器 4,优先级权重加法 id选择器+100/个 类/伪类选择器+10/个 ...
- 努比亚(nubia) M2青春版 NX573J 解锁BootLoader 并进入临时recovery ROOT
努比亚(nubia) M2青春版 NX573J 解锁BootLoader 并进入临时recovery ROOT 工具下载链接:https://pan.baidu.com/s/1NfRTdXtdAZRi ...
- href标签中target的几个属性值
没有指定<a>的target属性值时,默认是"_blank,可以参考以下代码的设置来控制<a href="#" target="_blank& ...
- VS2012快捷操作功能
VS2003用了6年,感情深厚,最近换工作刚刚接触VS2010,使用一个月感觉VS2010在人性化方面的功能实在是太强悍了,大大提高了写代码的效率,就如同魔兽世界里的快捷键操作一样,左手抚键右手摸鼠, ...
- 协议(Protocol) 和代理(Delegate)
1.概念与组成 delegate是iOS中一种常见的设计模式,是一种消息传递的的方式,常见的消息传递方式还有以下几种: 通知:在iOS中由通知中心进行消息接收和消息广播,是一种一对多的消息传递方式. ...
- VMWare虚拟机移动
1. 背景: 虚拟机:VM3 原安装路径:C:\Users\Administrator\Documents\Virtual Machines 移动到目标路径:D:\Virtual Machines ...
- .Net并行计算支持嵌套事务的方法
问题背景 一年前,我们开始利用.Net 4.0的TPL(Task Parallel Library)并行计算技术对复杂计算的功能节点进行性能优化,这些复杂计算往往会包含大量对数据库的操作.在应用TPL ...