xmrarray.h

来自「上传的是有关mfc的详细介绍」· C头文件 代码 · 共 72 行

H
72
字号
// XMRArray.h: interface for the XMRArray class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_XMRARRAY_H__D7AEE90B_335B_4B46_ABD3_73B8B54ED4E7__INCLUDED_)
#define AFX_XMRARRAY_H__D7AEE90B_335B_4B46_ABD3_73B8B54ED4E7__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <vector>

template<typename T>
class XMRArray :public std::vector<T> 
{
public:
	XMRArray() { }	

	int Add(const T& newElement)
	{
		push_back(newElement);
		return (int)this->size() - 1;
	}

	int GetSize() const
	{
		return (int)this->size();
	}

	T GetAt(int nIndex) const
	{
		return this->at(nIndex);
	}

	void RemoveAt(int nIndex, int nCount=1)
	{
		int nSize = this->size();
		if(nIndex >= nSize)
		{
			return;
		}

		// modified by zengzm 2005-9-12 增加判断,防止nCount<0
		int nEnd = nIndex + nCount;
		if (nEnd > nSize)
		{
			nEnd=nSize;
		}
		this->erase(this->begin() + nIndex, this->begin() + nEnd);
	}

	//! \brief 移除所有元素,同时清空所有空间。
	void RemoveAll()
	{
		this->clear(); 		
		FreeExtra();
	}

	//! \brief 释放数组中多余的内存空间。
	//! \remarks 删除数组中除有效元素占用外的空间。
	void FreeExtra()
	{
		if(this->capacity()>this->size()) {
			std::vector<T> vctTemp(this->begin(),this->end());
			swap(vctTemp);
		}
	}
};

#endif // !defined(AFX_XMRARRAY_H__D7AEE90B_335B_4B46_ABD3_73B8B54ED4E7__INCLUDED_)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?