VC :模板类

//基于控制台的程序
//模板类:CTypedPtrlist、CTypedPtrArray、CTypedPtrMap;添加MFC的支持,方法如下:
//Project|Settings命令,弹出Project Setting对话框,在General标签下的“Microsoft Foundation Classes”下选择Used MFC in Shared DLL项。
#include <iostream.h>
#include <afxtempl.h>//引入头文件
class BASE  //定义一个类
{
public:
 int x;  //类的成员变量
 int y;
};
CTypedPtrList <CPtrList,BASE *> m_list; //模板列表
void main()
{
 int i=0;
 int x;
 int y;
 while(i==0)
 {
  BASE *base1=new BASE;
  cin>>x>>y;//输入数据
  base1->x=x;
  base1->y=y;
  m_list.AddTail(base1);//添加到链表的尾部
  cout<<"输入0继续,输入其他则推出"<<endl;
  cin>>i;
 }
 POSITION pos=m_list.GetHeadPosition();//得到头部位置
 while(pos!=NULL)
 {
  BASE *base2=new BASE;
  base2=m_list.GetNext(pos);//得到下一个位置
  cout<<base2->x<<base2->y<<endl;//输出数据
 }
}

posted @ 2013-03-02 23:26  慧由心生  阅读(392)  评论(0编辑  收藏  举报