INT_PTR Index = 0;
CArray <CPoint , CPoint &> arrPoint;
//判断数组是否为空
BOOL bRet = arrPoint.IsEmpty();
//添加元素到数组
for (Index = 0; Index < 100; Index++)
{
CPoint pt;
pt.SetPoint(Index , Index); //设置元素x y坐标为当前索引值
arrPoint.Add(pt);
}
//检测当前元素总数
int nGetCount = arrPoint.GetCount();
int nGetSize = arrPoint.GetSize();
//检测元素最大数
int maxIdx = arrPoint.GetUpperBound();
//判断当前是否为空
bRet = arrPoint.IsEmpty();
//读取指定元素
CPoint pt = arrPoint.GetAt(10);
//删除指定元素 *重载函数 第二个参数为非选参数 为删除多少个元素 默认为1
arrPoint.RemoveAt(10,5);
//再次获取元素总数
nGetCount = arrPoint.GetCount();
//再次获取最大元素
maxIdx = arrPoint.GetUpperBound();
//通过GetAt成员函数读取数据
pt = arrPoint.GetAt(10);
//通过数组下标读取数据
pt = arrPoint[11];
//遍历数组
for (Index = 0; Index < arrPoint.GetCount(); Index++)
{
CPoint m_pt = arrPoint.GetAt(Index);
m_pt.x = 123; //错误的设置数据方法 只是修改的临时获取到固定索引的值 并没有得到获取固定索引的值在内存中的位置
}
//正确做法 通过SetAt设置索引为5的值
pt.x = pt.y = 105;
arrPoint.SetAt(5,pt);
//通过数组下标设置索引为6的值
pt.x = pt.y = 106;
arrPoint[6] = pt;
//通过InsertAt函数插入数据
pt.x = pt.y = 107;
arrPoint.InsertAt(7,pt);
//再次遍历查看数组中CPoint数组
for (Index = 0; Index < arrPoint.GetCount(); Index++)
{
pt = arrPoint.GetAt(Index);
}
//移除所有的数据
arrPoint.RemoveAll();
//获取移除所有数据以后的总数
nGetCount = arrPoint.GetCount();
//再次判断是否为空
bRet = arrPoint.IsEmpty();
CArray 是MFC从C++标准制定以后设计的动态数组模版类
template < class TYPE, class ARG_TYPE = const TYPE& > class CArray : public CObject
两个参数,其中带ARG的是传入参数,另一个为返回类型,本实例代码包含大多数常用操作,写了注释方便理解。
本文详细介绍了MFC框架中CArray模板类的基本用法,包括数组的创建、元素的添加与删除、遍历及修改等操作,并提供了实用的代码示例。
20

被折叠的 条评论
为什么被折叠?



