CList使用说明

http://blog.sina.com.cn/s/blog_70441c8e0101aiwf.html


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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值