MFC集合类之CList

之前我们先以CStringList为例演示下列成员方法:

AddTail, AddHead, GetHeadPosition, GetTailPosition, GetPrev(), GetNext()

我们看到如何向一个CList类中增加数据:可以从两端增加,以及如何遍历整个CList……

 

[cpp]  view plain copy
  1. #include <afxwin.h>  
  2. #include <afxtempl.h>  
  3. #include <iostream>  
  4. using std::cout;  
  5. using std::endl;  
  6. int main() {  
  7.     const TCHAR str[][10] = {  
  8.         _T("number 1"),  
  9.         _T("number 2"),  
  10.         _T("number 3"),  
  11.         _T("number 4"),  
  12.         _T("number 5")  
  13.     };  
  14.     CStringList list;  
  15.     for(int i = 0; i < sizeof(str) / sizeof(str[0]); i++)  
  16.         list.AddTail(str[i]);  
  17.     POSITION pos = list.GetHeadPosition();  
  18.     while(pos != NULL) {  
  19.         CString string = list.GetNext(pos);  
  20.         TRACE(_T("%s/n"), string);  
  21.     }  
  22.     TRACE("之后呢:/n");  
  23.     for(int i = 0; i < sizeof(str) / sizeof(str[0]) - 1; i++)  
  24.         list.AddHead(str[i]);  
  25.     pos = list.GetHeadPosition();  
  26.     while(pos != NULL) {  
  27.         CString string = list.GetNext(pos);  
  28.         TRACE(_T("%s/n"), string);  
  29.     }  
  30.     TRACE("再之后呢:/n");  
  31.     pos = list.GetTailPosition();  
  32.     while(pos != NULL) {  
  33.         CString string = list.GetPrev(pos);  
  34.         TRACE(_T("%s/n"), string);  
  35.     }  
  36. }  

 

 

GetAt与SetAt方法:

[cpp]  view plain copy
  1. #include <afxwin.h>  
  2. #include <afxtempl.h>  
  3. #include <iostream>  
  4. using std::cout;  
  5. using std::endl;  
  6. int main() {  
  7.     const TCHAR str[][10] = {  
  8.         _T("number 1"),  
  9.         _T("number 2"),  
  10.         _T("number 3"),  
  11.         _T("number 4"),  
  12.         _T("number 5")  
  13.     };  
  14.     CStringList list;  
  15.     for(int i = 0; i < sizeof(str) / sizeof(str[0]); i++)  
  16.         list.AddTail(str[i]);  
  17.     POSITION pos = list.GetTailPosition();  
  18.     CString string = list.GetAt(pos);  
  19.     TRACE(_T("%s/n"), string);  
  20.     TRACE(_T("after SetAt: /n"));  
  21.     list.SetAt(pos, _T("hehehehe"));  
  22.     string = list.GetAt(pos);  
  23.     TRACE(_T("%s/n"), string);  
  24. }  

 

 

RemoveAt, InsertBefore, InsertAfter方法:

[cpp]  view plain copy
  1. #include <afxwin.h>  
  2. #include <afxtempl.h>  
  3. #include <iostream>  
  4. using std::cout;  
  5. using std::endl;  
  6. int main() {  
  7.     const TCHAR str[][10] = {  
  8.         _T("number 1"),  
  9.         _T("number 2"),  
  10.         _T("number 3"),  
  11.         _T("number 4"),  
  12.         _T("number 5")  
  13.     };  
  14.     CStringList list;  
  15.     for(int i = 0; i < sizeof(str) / sizeof(str[0]); i++)  
  16.         list.AddTail(str[i]);  
  17.     POSITION pos = list.GetTailPosition();  
  18.     list.RemoveAt(pos);  
  19.     pos = list.GetTailPosition();  
  20.     while(pos != NULL) {  
  21.         CString str = list.GetPrev(pos);  
  22.         TRACE(_T("%s/n"), str);  
  23.     }  
  24.     pos = list.GetTailPosition();  
  25.     list.InsertBefore(pos, _T("huhu!!!"));  
  26.     list.InsertAfter(pos, _T("i am the last!"));  
  27.     pos = list.GetHeadPosition();  
  28.     TRACE(_T("经过InsertBefore和InsertAfter: /n"));  
  29.     while(pos != NULL) {  
  30.         CString str = list.GetNext(pos);  
  31.         TRACE(_T("%s/n"), str);  
  32.     }  
  33. }  

 

 

 

以及采用模板特性的CList:

头文件:

[cpp]  view plain copy
  1. #pragma once  
  2. class CPoint3D {  
  3. public:  
  4.     int x, y, z;  
  5. public:  
  6.     CPoint3D() : x(0), y(0), z(0) {}  
  7.     CPoint3D(int xx, int yy, int zz) : x(xx), y(yy), z(zz) {}  
  8.     int operator==(const CPoint3D& pt) const {  
  9.         return (pt.x == x && pt.y == y && pt.z == z);  
  10.     }  
  11. };  

 

实现:

[cpp]  view plain copy
  1. #include <afxwin.h>  
  2. #include <afxtempl.h>  
  3. #include <iostream>  
  4. #include "CListDemo.h"  
  5. using std::cout;  
  6. using std::endl;  
  7. int main() {  
  8.     CList<CPoint, CPoint&> list;  
  9.     for(int i = 0; i < 10; i++)  
  10.         list.AddTail(CPoint(i, i * i));  
  11.     POSITION pos = list.GetHeadPosition();  
  12.     while(pos != NULL) {  
  13.         CPoint pt = list.GetNext(pos);  
  14.         TRACE(_T("x = %d/ty = %d/n"), pt.x, pt.y);  
  15.     }  
  16.     TRACE("自定义类使用:/n");  
  17.     CList<CPoint3D, CPoint3D&> list1;  
  18.     for(int i = 0; i < 10; i++)  
  19.         list1.AddHead(CPoint3D(i, i * i, i * i * i));  
  20.     pos = list1.GetHeadPosition();  
  21.     while(pos != NULL) {  
  22.         CPoint3D pt = list1.GetNext(pos);  
  23.         TRACE(_T("x = %d/ty = %d/tz = %d/n"),  
  24.             pt.x, pt.y, pt.z);  
  25.     }  
  26.     pos = list1.Find(CPoint3D(6, 36, 216));  
  27.     CPoint3D point3DFind = list1.GetAt(pos);  
  28.     TRACE("查找展示,嘿嘿:");  
  29.     TRACE(_T("x = %d/ty = %d/tz = %d/n"), point3DFind.x, point3DFind.y, point3DFind.z);  
  30. }  

在这里我使用了Find成员方法来对模板CList进行查找,所以我们必须在自定义的类中重载“==”运算符,看到了头文件

当中的operator==方法吧……


FROM: http://blog.csdn.net/netrookie/article/details/5317916

MSDN:http://msdn.microsoft.com/zh-cn/library/bxde0zae.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值