C list函数详解
c语言中list的实现方式

c语言中list的实现方式
在C语言中,实现列表(list)的方式有多种。
其中包括使用
数组、使用指针和动态内存分配以及使用结构体等方法。
首先,可以使用数组来实现列表。
这种方法需要预先定义数组
的大小,然后在数组中存储列表元素。
使用数组实现列表的好处是
访问元素速度快,但缺点是数组大小固定,不够灵活。
其次,可以使用指针和动态内存分配来实现列表。
这种方法可
以在运行时动态分配内存,使列表的大小可以根据需要进行调整。
使用指针和动态内存分配实现列表的好处是灵活性高,但需要手动
管理内存,存在内存泄漏的风险。
另外,还可以使用结构体来实现列表。
通过定义一个包含数据
和指向下一个节点的指针的结构体,可以实现链表(linked list)。
链表可以是单向的,也可以是双向的,具有灵活的插入和删除操作,但访问元素的速度相对较慢。
除了上述方法,还可以结合使用数组和指针,或者使用其他数
据结构来实现列表,如栈、队列等。
每种实现方式都有其优缺点,
选择合适的实现方式取决于具体的需求和应用场景。
总的来说,在C语言中,实现列表的方式有多种多样,开发人员可以根据实际情况选择最适合的方式来实现列表。
c语言中list的用法

C语言中list的用法1.简介在C语言中,l is t是一种常用的数据结构,用于存储和管理多个元素。
它类似于数组,但具有更强大的灵活性和功能。
本文将介绍C语言中l i st的使用方法,包括创建、添加、删除和遍历等操作。
2.创建lis t要使用l is t,首先需要定义一个结构体来表示l is t的节点,节点中包含数据元素和指向下一个节点的指针。
然后,使用指向该结构体的指针来表示整个l is t。
以下是创建l is t的基本代码:t y pe de fs tr uc tN ode{i n td at a;s t ru ct No de*n ex t;}N od e;t y pe de fs tr uc t{N o de*h ea d;}L is t;3.添加元素要向li st中添加元素,可以使用以下代码:v o id ad dE le me nt(Li s t*li st,i nt ne wDa t a){N o de*n ew No de=(Nod e*)ma ll oc(s iz eof(No de));n e wN od e->d at a=new D at a;n e wN od e->n ex t=NUL L;i f(l is t->h ea d==NU L L){l i st->he ad=n ew Nod e;}e ls e{N o de*c ur re nt No de=l is t->h ea d;w h il e(cu rr en tN ode->n ex t!=N UL L){c u rr en tN od e=cu rre n tN od e->n ex t;}c u rr en tN od e->n ext=ne wN od e;}}4.删除元素要从li st中删除元素,可以使用以下代码:v o id re mo ve El em ent(Li st*l is t,in tta r ge t){ N o de*c ur re nt No de=l is t->h ea d;N o de*p re vN od e=NUL L;w h il e(cu rr en tN ode!=N UL L){i f(c ur re nt No de->d a ta==ta rg et){i f(p re vN od e==N ULL){l i st->he ad=c ur ren t No de->ne xt;}e ls e{p r ev No de->ne xt=cu r re nt No de->ne xt;}f r ee(c ur re nt No de);b r ea k;}p r ev No de=c ur re ntN o de;c u rr en tN od e=cu rre n tN od e->n ex t;}}5.遍历lis t要遍历l is t中的所有元素,可以使用以下代码:v o id tr av er se Li st(L is t*li st){N o de*c ur re nt No de=l is t->h ea d;w h il e(cu rr en tN ode!=N UL L){p r in tf("%d",cu rre n tN od e->d at a);c u rr en tN od e=cu rre n tN od e->n ex t;}}6.示例下面是一个使用l ist的示例:#i nc lu de<s td io.h>#i nc lu de<s td li b.h>t y pe de fs tr uc tN ode{i n td at a;s t ru ct No de*n ex t;}N od e;t y pe de fs tr uc t{N o de*h ea d;}L is t;v o id ad dE le me nt(Li s t*li st,i nt ne wDa t a){N o de*n ew No de=(Nod e*)ma ll oc(s iz eof(No de)); n e wN od e->d at a=new D at a;n e wN od e->n ex t=NUL L;i f(l is t->h ea d==NU L L){l i st->he ad=n ew Nod e;}e ls e{N o de*c ur re nt No de=l is t->h ea d;w h il e(cu rr en tN ode->n ex t!=N UL L){c u rr en tN od e=cu rre n tN od e->n ex t;}c u rr en tN od e->n ext=ne wN od e;}}v o id re mo ve El em ent(Li st*l is t,in tta r ge t){N o de*c ur re nt No de=l is t->h ea d;N o de*p re vN od e=NUL L;w h il e(cu rr en tN ode!=N UL L){i f(c ur re nt No de->d a ta==ta rg et){i f(p re vN od e==N ULL){l i st->he ad=c ur ren t No de->ne xt;}e ls e{p r ev No de->ne xt=cu r re nt No de->ne xt; }f r ee(c ur re nt No de);b r ea k;}p r ev No de=c ur re ntN o de;c u rr en tN od e=cu rre n tN od e->n ex t;}}v o id tr av er se Li st(L is t*li st){N o de*c ur re nt No de=l is t->h ea d;w h il e(cu rr en tN ode!=N UL L){p r in tf("%d",cu rre n tN od e->d at a);c u rr en tN od e=cu rre n tN od e->n ex t;}}i n tm ai n(){L i st my Li st;m y Li st.h ea d=NU LL;a d dE le me nt(&my Lis t,5);a d dE le me nt(&my Lis t,10);a d dE le me nt(&my Lis t,15);r e mo ve El em en t(&my L is t,10);t r av er se Li st(&myL i st);r e tu rn0;}7.总结使用li st可以轻松地管理多个元素,实现灵活的数据存储和操作。
c链表库函数

c链表库函数全文共四篇示例,供读者参考第一篇示例:C语言是一种广泛应用于系统编程的高级语言,而链表(Linked List)是C语言中常用的数据结构之一。
在C语言中,链表并不像数组一样有现成的库函数可以直接调用,需要通过自定义函数来实现链表的操作。
为了方便使用链表,不少开发者封装了链表操作的库函数,提供了一些常用的链表操作接口,以供开发者使用。
本文将介绍一些常见的C链表库函数及其用法。
一、链表的概念及基本操作链表是一种线性表的存储结构,由若干节点(Node)组成,每个节点包含数据域和指针域。
数据域用于存放数据,指针域用于指向下一个节点。
链表的最后一个节点指针域为空(NULL),表示链表的末尾。
常见的链表操作包括创建链表、插入节点、删除节点、遍历链表、查找节点等。
下面我们来看看C语言中常用的链表库函数。
二、常见的C链表库函数1. 创建链表在C语言中,创建链表的函数通常包括初始化链表头节点和链表节点的操作。
```#include <stdio.h>#include <stdlib.h>//定义链表节点typedef struct node {int data;struct node* next;} Node;2. 插入节点插入节点是链表操作中的重要操作,可以在链表的任意位置插入新节点。
常见的插入方式包括头部插入和尾部插入。
```//头部插入节点void insertNodeAtHead(Node* head, int data) {Node* newNode = (Node*)malloc(sizeof(Node));newNode->data = data;newNode->next = head->next;head->next = newNode;}以上是常见的C链表库函数,这些函数可以帮助我们更方便地操作链表。
在实际开发中,可以根据需要自定义更多的链表操作函数,以满足具体的需求。
list类函数

list类函数List类函数在编程中起到了非常重要的作用,可以用于存储和操作一系列的数据。
下面将介绍几个常用的List类函数,并详细解释它们的功能和用法。
1. append()函数append()函数用于向List的末尾添加元素。
例如,我们有一个List,里面存储了一些数字,我们可以使用append()函数来添加新的数字。
示例代码:numbers = [1, 2, 3, 4, 5]numbers.append(6)print(numbers)输出结果:[1, 2, 3, 4, 5, 6]2. insert()函数insert()函数用于在指定的位置插入元素。
它接受两个参数,第一个参数是要插入的位置,第二个参数是要插入的元素。
示例代码:fruits = ['apple', 'banana', 'orange']fruits.insert(1, 'grape')print(fruits)输出结果:['apple', 'grape', 'banana', 'orange']3. remove()函数remove()函数用于从List中移除指定的元素。
如果有多个相同的元素,它只会移除第一个匹配的元素。
示例代码:numbers = [1, 2, 3, 4, 5]numbers.remove(3)print(numbers)输出结果:[1, 2, 4, 5]4. pop()函数pop()函数用于移除List中指定位置的元素,并返回被移除的元素。
如果没有指定位置,默认移除最后一个元素。
示例代码:fruits = ['apple', 'banana', 'orange']removed_fruit = fruits.pop(1)print(fruits)print(removed_fruit)输出结果:['apple', 'orange']banana5. sort()函数sort()函数用于对List中的元素进行排序。
VC中CList用法

VC中CList用法(2009-05-22 16:26:32)转载▼分类:C&C++标签:杂谈CList是一个双向链表类。
1、头文件名不可少Clist类定义在Afxtempl.h 头文件中,因此在使用该类时,需要加这个头文件名。
2、理解CList的声明和构造方法CList的声明如下:template< class TYPE, class ARG_TYPE >class CList : public CObject由此,我们知道CList是一个模版类,那么他的两个class是什么意思呢?下面看一个例子:CList<CString ,CString&> list;//链表对象1CList<CString,CString> list2;//链表对象2这里的第一个参数CString是实例化的类型,第二个参数是类的成员函数的参数的调用形式,通常是类型引用,当然也可以是对象,而不是引用。
对象和引用的区别,可以看一下C++基础知识方面的书。
3、使用CList的迭代器迭代器是近年在链表使用中的一个常用技术。
如果大家学过java,就会知道,在java中,会有专门的一个迭代器类,这个迭代器类用来访问Collect中的数据元素。
在mfc中,在CList中,定义了几个迭代器的函数。
他们方便了我们去访问数据元素。
GetHeadPosition Returns the position of the head element of the list. GetTailPosition Returns the position of the tail element of the list.GetNext Gets the next element for iterating.GetPrev Gets the previous element for iterating.以上是四个迭代器函数。
c语言list用法

c语言list用法在C语言中,没有内置的List数据类型,但是可以使用数组来实现类似的功能。
下面是一种用数组实现List的常见方式:1. 定义一个结构体来表示List和相关的元素:ctypedef struct{int capacity; List最大容量int size; List当前元素个数int *data; List元素数组} List;2. 初始化List:cList* initList(int capacity){List *list = (List*)malloc(sizeof(List)); 分配内存list->capacity = capacity; 设置容量list->size = 0; 初始元素个数为0list->data = (int*)malloc(sizeof(int) * capacity); 分配数组内存return list;}3. 添加元素到List的末尾:cvoid add(List *list, int element){if(list->size == list->capacity){List已满,需要扩容list->capacity *= 2;list->data = (int*)realloc(list->data, sizeof(int) * list->capacity);}list->data[list->size++] = element;}4. 获取List指定位置的元素:cint get(List *list, int index){if(index < 0 index >= list->size){printf("Index out of range.\n");return -1; 返回一个错误值}return list->data[index];5. 删除List末尾的元素:cvoid removeLast(List *list){if(list->size > 0){list->size;}}6. 销毁List:cvoid destroyList(List *list){free(list->data); 释放数组内存free(list); 释放List内存}使用示例:cint main(){List *list = initList(5); 初始化容量为5的Listadd(list, 10);add(list, 20);add(list, 30);printf("List[0]: %d\n", get(list, 0)); 输出:List[0]: 10removeLast(list);printf("List[2]: %d\n", get(list, 2)); 输出:Index out of range. List[2]: -1destroyList(list); 销毁Listreturn 0;}这只是一种使用数组实现List的方式,根据实际需求可以进行相应的修改和扩展。
VC中CList用法及其成员的使用

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

c语言list定义C语言中的List(链表)定义和使用链表(List)是一种常见的数据结构,它在C语言中被广泛使用。
链表是由节点(Node)组成的,每个节点包含数据以及指向下一个节点的指针。
相比于数组,链表的长度可以动态调整,更加灵活。
1. 链表的定义与结构在C语言中,我们可以使用结构体来定义链表的节点。
一个简单的链表节点定义如下:```cstruct Node {int data; // 存储的数据struct Node* next; // 指向下一个节点的指针};```2. 创建链表要创建一个链表,我们首先需要定义一个指向链表头部的指针,通常称为头指针(head)。
创建一个空链表的步骤如下:```cstruct Node* head = NULL; // 初始化头指针为空```3. 插入节点链表的插入操作通常包括在链表的头部或尾部插入节点,以及在指定位置插入节点。
下面是几个常见的插入操作示例:在链表头部插入节点:```cstruct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // 创建新节点newNode->data = 1; // 设置新节点的数据newNode->next = head; // 将新节点的next指针指向当前头节点head = newNode; // 更新头指针,使其指向新节点```在链表尾部插入节点:```cstruct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // 创建新节点newNode->data = 2; // 设置新节点的数据newNode->next = NULL; // 将新节点的next指针设置为NULL,表示链表的末尾struct Node* cur = head;while (cur->next != NULL) {cur = cur->next; // 遍历链表,找到最后一个节点}cur->next = newNode; // 将新节点连接到最后一个节点的next 指针上```在指定位置插入节点:```cstruct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // 创建新节点newNode->data = 3; // 设置新节点的数据struct Node* cur = head;while (cur->data != 2) {cur = cur->next; // 遍历链表,找到要插入节点的位置}newNode->next = cur->next; // 将新节点的next指针指向原位置的节点cur->next = newNode; // 将新节点连接到指定位置的节点的next指针上```4. 删除节点删除链表中的节点通常包括删除头节点、尾节点以及指定位置的节点。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一、构造、析构函数、=运算符1、功能:声明list容器。
4种方式list<int> first;// empty list of intslist<int> second (4,100);// four ints with value 100。
4个100list<int> third (),());// iterating through secondlist<int> fourth (third);// a copy of third2、功能:注销list。
~list ( );3、原型:list1 = list2;功能:将list2赋值给list1,包括list的所有元素以及list2的size返回值:this指针二、返回迭代器类的函数begin、end、rbegin、rend举例:Begin指向第一个元素,黄色箭头。
end是最后一个元素的后一个位置,黑色箭头。
Begin和end一般一起使用,按正序输出list。
rbegin指逆序的第一个元素,即最后一个元素,蓝色箭头。
rend指逆序的最后一个元素的前一个位置,即第一个元素的前一个位置,红色箭头。
Rbegin和rend一般一起使用,用于逆序输出list。
三、list的容量相关的函数1、empty原型:bool empty ( ) const;功能:判断lsit是否为空,即size是否为0返回值:size为0,返回true,否则,返回false2、size原型:size_type size() const;功能:返回lsit中元素的个数返回值:size_type3、Max_size原型:size_type max_size () const;功能:返回lsit的最大容量返回值:4、resize原型:void resize ( size_type sz, T c = T());功能:重新分配lsit的大小。
如果sz小于目前的size就将多余的值删除;如果sz大于目前的size,就在增加容量,且用c填充。
例如:(5);//将size定为5(8,100);//将size定为8,多出的用100填充(12);//将size定为12四、获取元素1、front原型:reference front ( );const_reference front ( ) const;功能:获取第一个元素返回值:第一个元素的值2、back原型:reference back ( );const_reference back ( ) const功能:获取最后一个元素返回值:最后一个元素五、修改lsit的函数1、assign原型:void assign ( InputIterator first, InputIterator last );void assign ( size_type n, const T& u)功能:为list重新分配空间并赋值。
将[first,last)范围内的值或者n次u值的拷贝赋给list 返回值:无2、push_front:从头插入一个元素。
pop_front:删除第一个元素push_back:在尾部插入一个元素。
pop_back:删除最后一个元素3、insert原型:iterator insert ( iterator position, const T& x );voidinsert ( iterator position, size_type n, const T& x );template <class InputIterator>voidinsert ( iterator position, InputIterator first, InputIterator last );功能:插入元素insert ( iterator position, const T& x ):在position位置处插入元素xinsert ( iterator position, size_type n, const T& x ):在position位置处开始插入n个xinsert ( iterator position, InputIterator first, InputIterator last ):在position位置处开始插入[first,last)范围内的元素。
返回值:只有第一个函数返回插入的元素所在位置4、erase原型:iterator erase ( iterator position );iterator erase ( iterator first, iterator last );功能:清除链表中position处或者[first,last)范围内的元素。
会减少list的size值。
返回值:清除的最后一个元素的下一个位置(迭代器)5、swap原型:void swap ( list<T,Allocator>& lst)功能:将两个lsit交换6、clear功能:清空list六、操作类的函数1、splice原型:设list2调用了splice函数void splice ( iterator position, list<T,Allocator>& x );将list x中的所有元素插入到调用该函数的list2的position处。
List x会被清空。
void splice ( iterator position, list<T,Allocator>& x, iterator i );将x中指向i的位置处的元素插入到list2的position处。
X会将i位置处的值删除。
void splice ( iterator position, list<T,Allocator>& x, iterator first, iterator last );将x中[first,last)位置处的元素插入到list2的position处。
功能:Move elements from list to list。
将一个lsit中的值移动到另一个list2、remove原型:void remove ( const T& value );功能:清除链表中特定的值value,lsit的size会相应减少。
返回值:无3、remove_if原型:template <class Predicate>void remove_if ( Predicate pred );功能:在满足Predicate pred返回true值时,移除元素。
pred可以是一个返回bool类型的函数,还可以是一个重写operator函数的类。
例如:// a predicate implemented as a function:bool single_digit (const int& value) { return (value<10); }// a predicate implemented as a class:class is_odd{public:bool operator() (const int& value) {return (value%2)==1; }};返回值:无4、unique原型:void unique ( );template <class BinaryPredicate>void unique ( BinaryPredicate binary_pred );按照规则binary_pred消除重复值。
例如:bool same_integral_part (double first, double second){ return ( int(first)==int(second) ); }// a binary predicate implemented as a class:class is_near{public:bool operator() (double first, double second){ return (fabs(first-second)<; }};调用:(same_integral_part);(is_near());功能:消除list中的重复元素返回值:5、merge原型:void merge ( list<T,Allocator>& x );template <class Compare>void merge ( list<T,Allocator>& x, Compare comp );功能:合并两个已经有序(同时为升序或降序)的list。
merge()组合起两个排好序的表。
如果一个表未排序,merge()仍然能产生出一个表,其中包含着原来两个表元素的并集。
当然,对结果的排序就没有任何保证了。
向splice()函数一样,merge()函数也不复制元素。
merge函数的作用是:将两个有序的序列合并为一个有序的序列。
函数参数:merge(first1,last1,first2,last2,result,compare);//firs1t为第一个容器的首迭代器,last1为第一个容器的末迭代器,first2为第二个容器的首迭代器,last2为容器的末迭代器,result为存放结果的容器,comapre为比较函数(可略写,默认为合并为一个升序序列)。
返回值:6、sort原型:void sort ( );template <class Compare>void sort ( Compare comp );功能:排序返回值:7、reverse功能:将list中的元素逆置。
返回值:。