VC中CList用法及其成员的使用

news/2024/7/6 1:59:56 标签: list, class, mfc, c
cle class="tags" href="/tags/CLASS.html" title=class>class="baidu_pl">
cle_content" class="tags" href="/tags/CLASS.html" title=class>class="article_content clearfix">
content_views" class="tags" href="/tags/CLASS.html" title=class>class="htmledit_views">

    初学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++基础知识方面的书。

///

class="tags" href="/tags/CLASS.html" title=class>class="label">MSDN:

class="tags" href="/tags/CLASS.html" title=class>class="label">使用时 要

class="tags" href="/tags/CLASS.html" title=class>class="label">#include <afxtempl.h>

class="tags" href="/tags/CLASS.html" title=class>class="label"> 

class="tags" href="/tags/CLASS.html" title=class>class="label">Construction

cellpadding="5" border="1">
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类型(自定义)的链表
如果不存入数据的话࿰c;刚建立的链表是空的࿰c;头尾都为空


class="tags" href="/tags/CLASS.html" title=class>class="label">Head/Tail Access

cellpadding="5" border="1">
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++) myList.AddTail(int(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++) myList.AddTail(int(i)); }//存入数据

int tmp=myList.GetTail();//tmp被赋予了9999


class="tags" href="/tags/CLASS.html" title=class>class="label">Operations

cellpadding="5" border="1">
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;链表数据个数减1࿰c;返回缩减前的头数据

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(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;链表数据个数减1࿰c;返回缩减前的尾数据

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(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;链表数据个数加1࿰c;返回新的链表头位置(POSITION);

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(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;链表数据个数加1࿰c;返回新的链表尾位置(POSITION);

例子:

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(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;其头尾皆变成空指针;


class="tags" href="/tags/CLASS.html" title=class>class="label">Iteration

cellpadding="5" border="1">
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++) myList.AddTail(int(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++) myList.AddTail(int(i)); }//存入数据

POSITION pos=myList.GetTailPosition();//获得链表尾的位置

color="#0000ff">GetNext

Gets the next element for iterating.

返回当前位置的数据࿰c;之后࿰c;位置后移一位;

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(i)); }//存入数据

POSITION pos=myList.GetHeadPosition();//获得链表头的位置

int tmp=myList.GetNext(pos);//tmp被赋予了头数据的值:0;同时pos指向第二个数据1;

color="#6f2e2a">GetPrev

Gets the previous element for iterating.

返回当前位置的数据࿰c;之后࿰c;位置前移一位;

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(i)); }//存入数据

POSITION pos=myList.GetTailPosition();//获得链表尾的位置

int tmp=myList.GetNext(pos);//tmp被赋予了尾巴数据的值:9999;同时pos指向倒数第二个数据9998;


class="tags" href="/tags/CLASS.html" title=class>class="label">Retrieval/Modification

cellpadding="5" border="1">
color="#0000ff">GetAt

Gets the element at a given position.

返回指定位置的数据;

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(i)); }//存入数据

POSITION pos=myList.GetTailPosition();//获得链表尾的位置࿰c;还可以继续改变pos࿰c;以指向其他数据

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++) myList.AddTail(int(i)); }//存入数据

POSITION pos=myList.GetTailPosition();//获得链表尾的位置࿰c;还可以继续改变pos࿰c;以指向其他数据

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++) myList.AddTail(int(i)); }//存入数据

POSITION pos=myList.GetTailPosition();//获得链表尾的位置
myList.RemoveAt(pos);//链表pos(尾部)位置的数据被清除࿰c;数据个数变为9999;


class="tags" href="/tags/CLASS.html" title=class>class="label">Insertion

cellpadding="5" border="1">
color="#6f2e2a">InsertBefore

Inserts a new element before a given position.

在指定位置前插入一个新数据࿰c;数据个数加1;

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(i)); }//存入数据

POSITION pos=myList.GetHeadPosition();//获得第一个数据的位置

myList.InsertBefore(pos,int(123));//在第一个数据前插入一个新数据: 123࿰c;同时数据个数变为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++) myList.AddTail(int(i)); }//存入数据

POSITION pos=myList.GetHeadPosition();//获得第一个数据的位置

myList.InsertAfter(pos,int(123));//在第一个数据后插入一个新数据: 123࿰c;同时数据个数变为10001


class="tags" href="/tags/CLASS.html" title=class>class="label">Searching

cellpadding="5" border="1">
color="#6f2e2a">Find

Gets the position of an element specified by pointer value.

返回指定数据对应的位置;

CList<int,int> myList;

for(int i=0;i<10000;i++) myList.AddTail(int(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;以此类推


class="tags" href="/tags/CLASS.html" title=class>class="label">Status

cellpadding="5" border="1">
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();


cle>

http://www.niftyadmin.cn/n/1536518.html

相关文章

[转帖]K8s 学习者绝对不能错过的最全知识图谱(内含 58个知识点链接)

K8s 学习者绝对不能错过的最全知识图谱&#xff08;内含 58个知识点链接&#xff09; https://www.cnblogs.com/alisystemsoftware/p/11429164.html需要加强学习呢. 作者 | 平名 阿里服务端开发技术专家 导读&#xff1a;Kubernetes 作为云原生时代的“操作系统”&#xff0c;熟…

vc下dll调试

http://blog.csdn.net/coding_hello/archive/2008/11/24/3364111.aspx 很多初学DLL和COM编程的人都为DLL的调试方法发愁。我结合自己学习COM的体验&#xff0c;总结DLL程序的调试如下。  DLL是一个不可运行的程序&#xff0c;它必须有其它程序的加载才可运行。故要调试DLL程序…

[转帖]Kubernetes中安装Helm及使用

Kubernetes中安装Helm及使用 2018年07月02日 17:41:09 灬勿忘丶心安 阅读数 3699更多 分类专栏&#xff1a; K8S版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循 CC 4.0 BY-SA 版权协议&#xff0c;转载请附上原文出处链接和本声明。 本文链接&#xff1a;https://blo…

VC中CString,int,string,char*之间的转换

1 CString,int,string,char*之间的转换 string 转 CString :CString.format("%s", string.c_str()); char 转 CString : CString.format("%s", char*); char 转 string : string s(char *); string 转 char * : char *p string.c_str(); CSt…

[转帖]从入门到实践:创作一个自己的 Helm Chart

从入门到实践&#xff1a;创作一个自己的 Helm Chart https://www.cnblogs.com/alisystemsoftware/p/11436469.html自己已经搭建好了 helm 和tiller 改天自己鼓捣一个gscloud相关的 helm chart 计划国庆节时搞定. 前言 我们平时在日常生活中会经常在不同的平台上与各种各样的…

[转帖]关于USB3.0以及type-C

忘记来源页面了..但是昨天晚上 usb 4.0 发布了 跟雷电C 安全一样的标准双向40gb 的带宽. 而且 以后只有usb type-C的接口了.我们办公机器上面的 typeC 同事用 ngff的 ssd 测试了下 只有 5gb 的带宽..略坑. 然后usb 3.0 改名叫做 usb 3.2 gen1usb3.1 改名叫做 usb 3.2 gen 2usb3…

原生 js 实现点击按钮复制文本

copy(){ const input document.createElement(input) document.body.appendChild(input) input.setAttribute(value," 你想要复制的内容 ") input.select() if (document.execCommand(copy)) { document.execCommand(copy) } document.body.removeChild(i…

[转帖]Windows安全认证是如何进行的?[Kerberos篇]

Windows安全认证是如何进行的&#xff1f;[Kerberos篇] NTLM 的简单看了一下 基本上了解了.. 这个KERBEROS 的看不太懂 感觉说的我也有点迷糊..虽然是对称加密的 但是不清不楚的..改天仔细再学习一下. 最近一段时间都在折腾安全&#xff08;Security&#xff09;方面的东西&am…