MFC集合

12 篇文章 0 订阅

不多说,运行一个例子看看。

// MFCCollection.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <afxtempl.h>

class CPoint3D{
public:
	int m_x;
	int m_y;
	int m_z;
	CPoint3D(){}
	CPoint3D(int x,int y,int z){
		m_x=x;
		m_y=y;
		m_z=z;
	}
public:
	operator==(const CPoint3D pt)const {
		return (m_x==pt.m_x)&&(m_y==pt.m_y)&&(m_z==pt.m_z);
	}
};
void CArrayTest(){
	CArray<CPoint3D,CPoint3D> ptArray;
	//set size
	ptArray.SetSize(100);
	//set element value
	for(int i=0;i<100;++i){
		ptArray.SetAt(i,CPoint3D(i,i,i));
	}
	ptArray.SetAtGrow(i,CPoint3D(i,i,i));
	ptArray.Add(CPoint3D(102,102,102));
	ptArray.InsertAt(0,CPoint3D(-2,-2,-2));
	ptArray.RemoveAt(0,5);
	ptArray.SetSize(10);
	ptArray.FreeExtra();	//free the extra memory
	int nSize=ptArray.GetSize();
	printf("size=%d\n",nSize);
	for(i=0;i<nSize;++i){
		printf("x=%d,y=%d,z=%d\n",
			ptArray[i].m_x,ptArray[i].m_y,ptArray[i].m_z);
	}
}
void CListTest(){
	CList<CPoint3D,CPoint3D>ptList;
	for(int i=0;i<10;++i){
		ptList.AddTail(CPoint3D(i,i,i));
	}
	ptList.SetAt(ptList.FindIndex(5),CPoint3D(66,66,66));
	ptList.RemoveAt(ptList.FindIndex(6));

	int nCount=ptList.GetCount();
	printf("count=%d\n",nCount);
/*	POSITION pos=ptList.GetHeadPosition();
	while(pos){
		CPoint3D pt=ptList.GetNext(pos);
			printf("x=%d,y=%d,z=%d\n",
			pt.m_x,pt.m_y,pt.m_z);
	}
	*/
	POSITION pos=ptList.GetTailPosition();
	while(pos){
		CPoint3D pt=ptList.GetPrev(pos);
			printf("x=%d,y=%d,z=%d\n",
			pt.m_x,pt.m_y,pt.m_z);
	}
	pos=ptList.Find(CPoint3D(4,4,4));
	if(pos){
		printf("CPoint(4,4,4) exist!\n");
	}
}
void CMapTest(){
	CMap<int,int,CPoint3D,CPoint3D> ptMap;
	ptMap.InitHashTable(121); //nSize=nCount*1.2+1; even-->odd
	int key=0;
	for(int i=0;i<100;++i){
		key=i*2+1;
		ptMap[key]=CPoint3D(i,i,i);
	}
	ptMap.SetAt(15,CPoint3D(77,77,77));
	ptMap[15]=CPoint3D(777,777,777);
	ptMap.RemoveKey(15);	//remove key--value
	int nCount=ptMap.GetCount();
	POSITION pos=ptMap.GetStartPosition();
	printf("count=%d\n",nCount);
	key=0;
	CPoint3D pt;
	while(pos){
		ptMap.GetNextAssoc(pos,key,pt);
		printf("ptMap[%d]=CPoint3D(%d,%d,%d)\n",
			key,pt.m_x,pt.m_y,pt.m_z);
	}
	if(ptMap.Lookup(175,pt)){
		printf("the element exist!\n");
		printf("ptMap[%d]=CPoint3D(%d,%d,%d)\n",
			175,pt.m_x,pt.m_y,pt.m_z);
	}
}
int main(int argc, char* argv[])
{
//	CArrayTest();
//	CListTest();
	CMapTest();
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值