http://blog.sina.com.cn/s/blog_70441c8e0101aiwf.html
这样就指定了CList中存放的是CPoint类型的引用;
CPtrList,CObList,CStringList都是具体的用于某种类型的集合类型
而CPtrList存放的是void类型的指针,取出的时候必须要强制转换;
CObList用于存放从CObject派生的数据类型的指针;
CStringList存放CString字符串类型,相当于CList
PS:有修订,必须加入MFC库
CList是通用型的列表类,你可以存放指定的数据类型,用法如下:
CList list;这样就指定了CList中存放的是CPoint类型的引用;
CPtrList,CObList,CStringList都是具体的用于某种类型的集合类型
而CPtrList存放的是void类型的指针,取出的时候必须要强制转换;
CObList用于存放从CObject派生的数据类型的指针;
CStringList存放CString字符串类型,相当于CList
// #include "stdafx.h"
// #include "test.h"
#include <afxtempl.h>
#include <iostream>
using namespace std;
class Point
{
public:
Point()
{
m_x = 0;
m_y = 0;
}
Point(int x, int y)
{
m_x = x;
m_y = y;
}
bool operator==(const Point& src) const
{
return ( (m_x == src.m_x) && (m_y == src.m_y) );
}
public:
int m_x;
int m_y;
};
typedef CList<Point, Point&> CPntLst;
int main()
{
CPntLst lst;
Point point, elem1, elem2;
elem1.m_x = 52;
elem1.m_y = 102;
elem2.m_x = 14;
elem2.m_y = 1621;
// add a element from tail, certainly, also can from head
lst.AddTail(elem1);
lst.AddTail(elem2);
// print the point count
cout<< "count: "<<lst.GetCount()<<endl;
// traverse the whole list
cout << "First time:\n";
size_t index = 0;
POSITION ps;
for( ps = lst.GetHeadPosition();ps;lst.GetNext(ps) )
{
// extract the point according the current position
point = lst.GetAt(ps);
printf("index: %d, m_x = %d, m_y = %d\n", index++, point.m_x, point.m_y);
}
// search the point which is equal to elem1
ps = lst.Find(elem1);
point = lst.GetAt(ps);
printf("elem1: m_x = %d, m_y = %d\n", point.m_x, point.m_y);
Point elem3(123, 123123);
// insert elem3 into the list after elem1
lst.InsertAfter(ps, elem3);
cout << "Second time:\n";
for( ps = lst.GetHeadPosition();ps;lst.GetNext(ps) )
{
// extract the point according the current position
point = lst.GetAt(ps);
printf("index: %d, m_x = %d, m_y = %d\n", index++, point.m_x, point.m_y);
}
// remove elem1
ps = lst.Find(elem1);
lst.RemoveAt(ps);
cout << "Third time:\n";
for( ps = lst.GetHeadPosition();ps;lst.GetNext(ps) )
{
// extract the point according the current position
point = lst.GetAt(ps);
printf("index: %d, m_x = %d, m_y = %d\n", index++, point.m_x, point.m_y);
}
// remove all the rest
lst.RemoveAll();
cout << "Fourth time:\n";
for( ps = lst.GetHeadPosition();ps;lst.GetNext(ps) )
{
// extract the point according the current position
point = lst.GetAt(ps);
printf("index: %d, m_x = %d, m_y = %d\n", index++, point.m_x, point.m_y);
}
return 0;
}