初学class="tags" href="/tags/MFC.html" title=mfc>mfc者c;往往对CList等class="tags" href="/tags/MFC.html" title=mfc>mfc的Collect类的使用感到迷惑c;在使用中经常会遇到许多问题c;导致对vc中的Collect类的使用产生了惧怕。以下c;就个人经历而言,告诉大家如何使用CList。
CList是一个双向链表类。
1、头文件名不可少
Cclass="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list类定义在Afxtempl.h 头文件中c;因此在使用该类时c;需要加这个头文件名。
2、理解CList的声明和构造方法
CList的声明如下:
template< class="tags" href="/tags/CLASS.html" title=class>class TYPE, class="tags" href="/tags/CLASS.html" title=class>class ARG_TYPE >class="tags" href="/tags/CLASS.html" title=class>class CList : public CObject
由此c;我们知道CList是一个模版类c;那么他的两个class="tags" href="/tags/CLASS.html" title=class>class是什么意思呢?
下面看一个例子:
CList<CString ,CString&> class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list;//链表对象1
CList<CString,CString> class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list2;//链表对象2
这里的第一个参数CString是实例化的类型c;第二个参数是类的成员函数的参数的调用形式c;通常是类型 引用c;当然也可以是对象c;而不是引用。对象和引用的区别c;可以看一下C++基础知识方面的书。
///
color="#0000ff">CList | Constructs an empty ordered class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list. 建立一个链表 example: CList<int,int> myList;//建立一个int链表 CList<CString,CString&> myList(16);//建立一个cstring的链表c;后面的16表示链表里面数据的个数c;如果不写的话c;可能是不限个数? CList<MYTYPE,MYTYPE&> myList;//建立一个MYTYPE类型(自定义)的链表 |
color="#0000ff">GetHead | Returns the head element of the class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list (cannot be empty). 返回链表的头数据 例子: CList<int,int> myList; for(int i=0;i<10000;i++) int tmp=myList.GetHead();//tmp被赋予了0 |
color="#6f2e2a">GetTail | Returns the tail element of the class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list (cannot be empty). 返回链表的尾数据 CList<int,int> myList; for(int i=0;i<10000;i++) int tmp=myList.GetTail();//tmp被赋予了9999 |
color="#6f2e2a">RemoveHead | Removes the element from the head of the class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list. 移除链表头数据c;链表数据个数减1c;返回缩减前的头数据 例子: CList<int,int> myList; for(int i=0;i<10000;i++) int tmp=myList.RemoveHead();//tmp被赋予了之前的头数据:0;同时数据个数变为9999; |
color="#6f2e2a">RemoveTail | Removes the element from the tail of the class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list. 移除链表尾数据c;链表数据个数减1c;返回缩减前的尾数据 例子: CList<int,int> myList; for(int i=0;i<10000;i++) int tmp=myList.RemoveTail();//tmp被赋予了之前的尾数据:9999;同时数据个数变为9999; |
color="#6f2e2a">AddHead | Adds an element (or all the elements in another class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list) to the head of the class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list (makes a new head). 在链表头处插入新数据c;链表数据个数加1c;返回新的链表头位置(POSITION); 例子: CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.AddHead(int(314));//链表有了一个新的头数据:314;同时链表个数变为10001;pos为新的头的位置; |
color="#0000ff">AddTail | Adds an element (or all the elements in another class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list) to the tail of the class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list (makes a new tail). 在链表尾处插入新数据c;链表数据个数加1c;返回新的链表尾位置(POSITION); 例子: CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.AddTail(int(314));//链表有了一个新的尾数据:314;同时链表个数变为10001;pos为新的尾的位置; |
color="#6f2e2a">RemoveAll | Removes all the elements from this class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list. 清空链表c;其头尾皆变成空指针; |
color="#0000ff">GetHeadPosition | Returns the position of the head element of the class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list. 返回链表头的位置; CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetHeadPosition();//获得链表头的位置 |
color="#6f2e2a">GetTailPosition | Returns the position of the tail element of the class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list. 返回链表尾的位置; CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetTailPosition();//获得链表尾的位置 |
color="#0000ff">GetNext | Gets the next element for iterating. CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetHeadPosition();//获得链表头的位置 int tmp=myList.GetNext(pos);//tmp被赋予了头数据的值:0;同时pos指向第二个数据1; |
color="#6f2e2a">GetPrev | Gets the previous element for iterating. CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetTailPosition();//获得链表尾的位置 int tmp=myList.GetNext(pos);//tmp被赋予了尾巴数据的值:9999;同时pos指向倒数第二个数据9998; |
color="#0000ff">GetAt | Gets the element at a given position. 返回指定位置的数据; CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetTailPosition();//获得链表尾的位置c;还可以继续改变posc;以指向其他数据 int tmp=myList.GetAt(pos);//tmp被赋予链表尾的数据 |
color="#6f2e2a">SetAt | Sets the element at a given position. CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetTailPosition();//获得链表尾的位置c;还可以继续改变posc;以指向其他数据 myList.SetAt(pos,int(222));//将链表尾部的数据赋成222 |
color="#6f2e2a">RemoveAt | Removes an element from this class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list, specified by position. 清除指定位置处的数据;同时数据个数减1; CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetTailPosition();//获得链表尾的位置 |
color="#6f2e2a">InsertBefore | Inserts a new element before a given position. 在指定位置前插入一个新数据c;数据个数加1; CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetHeadPosition();//获得第一个数据的位置 myList.InsertBefore(pos,int(123));//在第一个数据前插入一个新数据: 123c;同时数据个数变为10001 |
color="#6f2e2a">InsertAfter | Inserts a new element after a given position. 在指定位置后插入一个新数据c;数据个数加1; CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.GetHeadPosition();//获得第一个数据的位置 myList.InsertAfter(pos,int(123));//在第一个数据后插入一个新数据: 123c;同时数据个数变为10001 |
color="#6f2e2a">Find | Gets the position of an element specified by pointer value. 返回指定数据对应的位置; CList<int,int> myList; for(int i=0;i<10000;i++) POSITION pos=myList.Find(int(0));//获得0(链表头)的位置 |
color="#6f2e2a">FindIndex | Gets the position of an element specified by a zero-based index. 返回索引号对应的位置; POSITION pos=myList.FindIndex(0);//0表示链表头c;以此类推 |
color="#6f2e2a">GetCount | Returns the number of elements in this class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list. 返回链表的数据个数 int num=myList.GetCount();//获得链表的数据个数 |
color="#6f2e2a">IsEmpty | Tests for the empty class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LIST.html" title=list>list condition (no elements). 判定链表是否为空; 返回1表示链表是空c;返回0表示链表非空; BOOL empty=myList.IsEmpty(); |