MFC中 CArray(template)的应用

MFC中CArray代码的实现

template<class TYPE, class ARG_TYPE = const TYPE&>
class CArray : public CObject
{
public:
// Construction
	CArray();

// Attributes
	INT_PTR GetSize() const;
	INT_PTR GetCount() const;
	BOOL IsEmpty() const;
	INT_PTR GetUpperBound() const;
	void SetSize(INT_PTR nNewSize, INT_PTR nGrowBy = -1);

// Operations
	// Clean up
	void FreeExtra();
	void RemoveAll();

	// Accessing elements
	const TYPE& GetAt(INT_PTR nIndex) const;
	TYPE& GetAt(INT_PTR nIndex);
	void SetAt(INT_PTR nIndex, ARG_TYPE newElement);
	const TYPE& ElementAt(INT_PTR nIndex) const;
	TYPE& ElementAt(INT_PTR nIndex);

	// Direct Access to the element data (may return NULL)
	const TYPE* GetData() const;
	TYPE* GetData();

	// Potentially growing the array
	void SetAtGrow(INT_PTR nIndex, ARG_TYPE newElement);
	INT_PTR Add(ARG_TYPE newElement);
	INT_PTR Append(const CArray& src);
	void Copy(const CArray& src);

	// overloaded operator helpers
	const TYPE& operator[](INT_PTR nIndex) const;
	TYPE& operator[](INT_PTR nIndex);

	// Operations that move elements around
	void InsertAt(INT_PTR nIndex, ARG_TYPE newElement, INT_PTR nCount = 1);
	void RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1);
	void InsertAt(INT_PTR nStartIndex, CArray* pNewArray);

// Implementation
protected:
	TYPE* m_pData;   // the actual array of data
	INT_PTR m_nSize;     // # of elements (upperBound - 1)
	INT_PTR m_nMaxSize;  // max allocated
	INT_PTR m_nGrowBy;   // grow amount

public:
	~CArray();
	void Serialize(CArchive&);
#ifdef _DEBUG
	void Dump(CDumpContext&) const;
	void AssertValid() const;
#endif
}

CArray的实际使用

	OutputDebugString("*************************************************************************\n");
	OutputDebugString("*************************************************************************\n");
	OutputDebugString("*************************************************************************\n");
	OutputDebugString("*************************************************************************\n");

	class DemoStruct
	{
	public:
		int x;
		int y;
	};

	typedef CArray<DemoStruct*, DemoStruct*> DEMOSTRUCTARR;
	DEMOSTRUCTARR arr;

	// 和上面的两行代码功能一样 都是定义一个CArray<DemoStruct*, DemoStruct*>类型的arr 
	// CArray<DemoStruct*, DemoStruct*> arr;

	CString strOutput="";
	DemoStruct *dStruct = NULL;
	for (int i=1;i<=20;i++)
	{
		dStruct = new DemoStruct;
		dStruct->x=i;
		dStruct->y=i*10;
		arr.Add(dStruct);
		strOutput.Format("数组下标:%3d x:%4d y:%4d\n",i-1,dStruct->x,dStruct->y);
		OutputDebugString(strOutput);
	}
	OutputDebugString("*************************************************************************\n");

	// 获取数组元素的个数
	int isize = arr.GetSize();
	strOutput.Format("数组元素的个数:5d\n",isize);
	OutputDebugString(strOutput);
	OutputDebugString("*************************************************************************\n");

	dStruct = new DemoStruct;
	dStruct->x=10101;
	dStruct->y=11111;
	arr.InsertAt(1+7,dStruct);
	for (int i = 0; i < arr.GetCount(); i++)
	{
		strOutput.Format("数组下标:%3d x:%4d y:%4d\n",i,arr.GetAt(i)->x,arr.GetAt(i)->y);
		OutputDebugString(strOutput);
	}
	OutputDebugString("*************************************************************************\n");

	// 返回数组的上界,也就是最大索引值
	int iUpperBound=arr.GetUpperBound();
	strOutput.Format("数组的上界:5d\n",iUpperBound);
	OutputDebugString(strOutput);
	OutputDebugString("*************************************************************************\n");

	DemoStruct* p = arr.GetAt(3);
	strOutput.Format("数组下标:%3d x:%4d y:%4d\n",3,p->x,p->y);
	OutputDebugString(strOutput);
	OutputDebugString("*************************************************************************\n");

	for (int i = 0; i < arr.GetCount(); i++)
	{
		// 这就一位置arr中的每个元素必须是新new的,否则就会报堆栈错误
		delete arr.GetAt(i);
	}
	arr.RemoveAll();



输出结果:

*************************************************************************
*************************************************************************
*************************************************************************
*************************************************************************
数组下标:  0 x:   1 y:  10
数组下标:  1 x:   2 y:  20
数组下标:  2 x:   3 y:  30
数组下标:  3 x:   4 y:  40
数组下标:  4 x:   5 y:  50
数组下标:  5 x:   6 y:  60
数组下标:  6 x:   7 y:  70
数组下标:  7 x:   8 y:  80
数组下标:  8 x:   9 y:  90
数组下标:  9 x:  10 y: 100
数组下标: 10 x:  11 y: 110
数组下标: 11 x:  12 y: 120
数组下标: 12 x:  13 y: 130
数组下标: 13 x:  14 y: 140
数组下标: 14 x:  15 y: 150
数组下标: 15 x:  16 y: 160
数组下标: 16 x:  17 y: 170
数组下标: 17 x:  18 y: 180
数组下标: 18 x:  19 y: 190
数组下标: 19 x:  20 y: 200
*************************************************************************
数组元素的个数:5d
*************************************************************************
数组下标:  0 x:   1 y:  10
数组下标:  1 x:   2 y:  20
数组下标:  2 x:   3 y:  30
数组下标:  3 x:   4 y:  40
数组下标:  4 x:   5 y:  50
数组下标:  5 x:   6 y:  60
数组下标:  6 x:   7 y:  70
数组下标:  7 x:   8 y:  80
数组下标:  8 x:10101 y:11111
数组下标:  9 x:   9 y:  90
数组下标: 10 x:  10 y: 100
数组下标: 11 x:  11 y: 110
数组下标: 12 x:  12 y: 120
数组下标: 13 x:  13 y: 130
数组下标: 14 x:  14 y: 140
数组下标: 15 x:  15 y: 150
数组下标: 16 x:  16 y: 160
数组下标: 17 x:  17 y: 170
数组下标: 18 x:  18 y: 180
数组下标: 19 x:  19 y: 190
数组下标: 20 x:  20 y: 200
*************************************************************************
数组的上界:5d
*************************************************************************
数组下标:  3 x:   4 y:  40


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值