CList的一个使用例子
CArray内部是用了单线数组,而CList内部则用了双向链表,当数据量大的时候,效率是截然不同的。
下面是一个简单的CList使用例子,VC Win32 console工程。
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "test.h"
#include <afxtempl.h>
#include <iostream>
using namespace std;
class Point
{
public:
bool operator==(const Point& src) const
public:
};
typedef CList<Point, Point&>
int main()
{
// print the point count
cout<< "count: "<<lst.GetCount()<<endl;
// traverse the whole list
cout << "First time:"n";
size_t index = 0;
POSITION ps;
for( /*POSITION*/ ps = lst.GetHeadPosition();ps;lst.GetNext(ps) )
{
}
// 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) )
{
}
// remove elem1
ps = lst.Find(elem1);
lst.RemoveAt(ps);
cout << "Third time:"n";
for( ps = lst.GetHeadPosition();ps;lst.GetNext(ps) )
{
}
// remove all the rest
lst.RemoveAll();
cout << "Fourth time:"n";
for( ps = lst.GetHeadPosition();ps;lst.GetNext(ps) )
{
}
return 0;
}
浙公网安备 33010602011771号