之前我们先以CStringList为例演示下列成员方法:
AddTail, AddHead, GetHeadPosition, GetTailPosition, GetPrev(), GetNext()
我们看到如何向一个CList类中增加数据:可以从两端增加,以及如何遍历整个CList……
- #include <afxwin.h>
- #include <afxtempl.h>
- #include <iostream>
- using std::cout;
- using std::endl;
- int main() {
- const TCHAR str[][10] = {
- _T("number 1"),
- _T("number 2"),
- _T("number 3"),
- _T("number 4"),
- _T("number 5")
- };
- CStringList list;
- for(int i = 0; i < sizeof(str) / sizeof(str[0]); i++)
- list.AddTail(str[i]);
- POSITION pos = list.GetHeadPosition();
- while(pos != NULL) {
- CString string = list.GetNext(pos);
- TRACE(_T("%s/n"), string);
- }
- TRACE("之后呢:/n");
- for(int i = 0; i < sizeof(str) / sizeof(str[0]) - 1; i++)
- list.AddHead(str[i]);
- pos = list.GetHeadPosition();
- while(pos != NULL) {
- CString string = list.GetNext(pos);
- TRACE(_T("%s/n"), string);
- }
- TRACE("再之后呢:/n");
- pos = list.GetTailPosition();
- while(pos != NULL) {
- CString string = list.GetPrev(pos);
- TRACE(_T("%s/n"), string);
- }
- }
GetAt与SetAt方法:
- #include <afxwin.h>
- #include <afxtempl.h>
- #include <iostream>
- using std::cout;
- using std::endl;
- int main() {
- const TCHAR str[][10] = {
- _T("number 1"),
- _T("number 2"),
- _T("number 3"),
- _T("number 4"),
- _T("number 5")
- };
- CStringList list;
- for(int i = 0; i < sizeof(str) / sizeof(str[0]); i++)
- list.AddTail(str[i]);
- POSITION pos = list.GetTailPosition();
- CString string = list.GetAt(pos);
- TRACE(_T("%s/n"), string);
- TRACE(_T("after SetAt: /n"));
- list.SetAt(pos, _T("hehehehe"));
- string = list.GetAt(pos);
- TRACE(_T("%s/n"), string);
- }
RemoveAt, InsertBefore, InsertAfter方法:
- #include <afxwin.h>
- #include <afxtempl.h>
- #include <iostream>
- using std::cout;
- using std::endl;
- int main() {
- const TCHAR str[][10] = {
- _T("number 1"),
- _T("number 2"),
- _T("number 3"),
- _T("number 4"),
- _T("number 5")
- };
- CStringList list;
- for(int i = 0; i < sizeof(str) / sizeof(str[0]); i++)
- list.AddTail(str[i]);
- POSITION pos = list.GetTailPosition();
- list.RemoveAt(pos);
- pos = list.GetTailPosition();
- while(pos != NULL) {
- CString str = list.GetPrev(pos);
- TRACE(_T("%s/n"), str);
- }
- pos = list.GetTailPosition();
- list.InsertBefore(pos, _T("huhu!!!"));
- list.InsertAfter(pos, _T("i am the last!"));
- pos = list.GetHeadPosition();
- TRACE(_T("经过InsertBefore和InsertAfter: /n"));
- while(pos != NULL) {
- CString str = list.GetNext(pos);
- TRACE(_T("%s/n"), str);
- }
- }
以及采用模板特性的CList:
头文件:
- #pragma once
- class CPoint3D {
- public:
- int x, y, z;
- public:
- CPoint3D() : x(0), y(0), z(0) {}
- CPoint3D(int xx, int yy, int zz) : x(xx), y(yy), z(zz) {}
- int operator==(const CPoint3D& pt) const {
- return (pt.x == x && pt.y == y && pt.z == z);
- }
- };
实现:
- #include <afxwin.h>
- #include <afxtempl.h>
- #include <iostream>
- #include "CListDemo.h"
- using std::cout;
- using std::endl;
- int main() {
- CList<CPoint, CPoint&> list;
- for(int i = 0; i < 10; i++)
- list.AddTail(CPoint(i, i * i));
- POSITION pos = list.GetHeadPosition();
- while(pos != NULL) {
- CPoint pt = list.GetNext(pos);
- TRACE(_T("x = %d/ty = %d/n"), pt.x, pt.y);
- }
- TRACE("自定义类使用:/n");
- CList<CPoint3D, CPoint3D&> list1;
- for(int i = 0; i < 10; i++)
- list1.AddHead(CPoint3D(i, i * i, i * i * i));
- pos = list1.GetHeadPosition();
- while(pos != NULL) {
- CPoint3D pt = list1.GetNext(pos);
- TRACE(_T("x = %d/ty = %d/tz = %d/n"),
- pt.x, pt.y, pt.z);
- }
- pos = list1.Find(CPoint3D(6, 36, 216));
- CPoint3D point3DFind = list1.GetAt(pos);
- TRACE("查找展示,嘿嘿:");
- TRACE(_T("x = %d/ty = %d/tz = %d/n"), point3DFind.x, point3DFind.y, point3DFind.z);
- }
在这里我使用了Find成员方法来对模板CList进行查找,所以我们必须在自定义的类中重载“==”运算符,看到了头文件
当中的operator==方法吧……
FROM: http://blog.csdn.net/netrookie/article/details/5317916