双向循环链表listWord格式.docx
《双向循环链表listWord格式.docx》由会员分享,可在线阅读,更多相关《双向循环链表listWord格式.docx(9页珍藏版)》请在冰豆网上搜索。
list每次增加一个元素,不存在重新申请内存的情况,它的成本是恒定的。
而vector每当增加关键元素的时候,都需要重新申请新的更大的内存空间,会调用元素的自身的复制构造函数,存在构造成本。
在销毁旧内存的时候,会调用析构函数,存在析构成本。
所以在存储复杂类型和大量元素的情况下,list比vector更有优势!
List是一个双向链表,双链表既可以向前又向后链接他的元素。
List将元素按顺序储存在链表中.与向量(vector)相比,它允许快速的插入和删除,但是随机访问却比较慢。
assign()给list赋值
back()返回最后一个元素
begin()返回指向第一个元素的迭代器
clear()删除所有元素
empty()如果list是空的则返回true
end()返回末尾的迭代器
erase()删除一个元素
front()返回第一个元素
get_allocator()返回list的配置器
insert()插入一个元素到list中
max_size()返回list能容纳的最大元素数量
merge()合并两个list
pop_back()删除最后一个元素
pop_front()删除第一个元素
push_back()在list的末尾添加一个元素
push_front()在list的头部添加一个元素
rbegin()返回指向第一个元素的逆向迭代器
remove()从list删除元素
remove_if()按指定条件删除元素
rend()指向list末尾的逆向迭代器
resize()改变list的大小
reverse()把list的元素倒转
size()返回list中的元素个数
sort()给list排序
splice()合并两个list
swap()交换两个list
unique()删除list中重复的元素
List使用实例1
#include<
iostream>
list>
numeric>
algorithm>
usingnamespacestd;
//创建一个list容器的实例LISTINT
typedeflist<
LISTINT;
//创建一个list容器的实例LISTCHAR
char>
LISTCHAR;
intmain(intargc,char*argv[])
{
//--------------------------
//用list容器处理整型数据
//用LISTINT创建一个名为listOne的list对象
LISTINTlistOne;
//声明i为迭代器
LISTINT:
:
iteratori;
//从前面向listOne容器中添加数据
listOne.push_front
(2);
listOne.push_front
(1);
//从后面向listOne容器中添加数据
listOne.push_back(3);
listOne.push_back(4);
//从前向后显示listOne中的数据
cout<
<
"
listOne.begin()---listOne.end():
endl;
for(i=listOne.begin();
i!
=listOne.end();
++i)
cout<
*i<
"
;
endl;
//从后向后显示listOne中的数据
reverse_iteratorir;
listOne.rbegin()---listOne.rend():
for(ir=listOne.rbegin();
ir!
=listOne.rend();
ir++){
*ir<
}
//使用STL的accumulate(累加)算法
intresult=accumulate(listOne.begin(),listOne.end(),0);
Sum="
result<
------------------"
//用list容器处理字符型数据
//用LISTCHAR创建一个名为listOne的list对象
LISTCHARlistTwo;
LISTCHAR:
iteratorj;
//从前面向listTwo容器中添加数据
listTwo.push_front('
A'
);
B'
//从后面向listTwo容器中添加数据
listTwo.push_back('
x'
y'
//从前向后显示listTwo中的数据
listTwo.begin()---listTwo.end():
for(j=listTwo.begin();
j!
=listTwo.end();
++j)
char(*j)<
//使用STL的max_element算法求listTwo中的最大元素并显示
j=max_element(listTwo.begin(),listTwo.end());
ThemaximumelementinlistTwois:
char(*j)<
return0;
List使用实例2
list:
Linkedlistofvariables,structorobjects.Insert/removeanywhere.
Twoexamplesaregiven:
1.ThefirstSTLexampleisfordatatype
int
2.Thesecondforalistof
class
instances.
Theyareusedtoshowasimpleexampleandamorecomplexrealworldapplication.
1.LetsstartwithasimpleexampleofaprogramusingSTLforalinkedlist:
//Simpleexampleusestypeint
intmain()
list<
L;
L.push_back(0);
//Insertanewelementattheend
L.push_front(0);
//Insertanewelementatthebeginning
L.insert(++L.begin(),2);
//Insert"
2"
beforepositionoffirstargument
//(Placebeforesecondargument)
L.push_back(5);
L.push_back(6);
for(i=L.begin();
=L.end();
++i)cout<
cout<
return0;
Compile:
g++example1.cpp
Run:
./a.out
Output:
02056
2.TheSTLtutorialsandtextsseemtogivesimpleexampleswhichdonotapplytotherealworld.Thefollowingexampleisforadoublylinkedlist.Sinceweareusingaclassandwearenotusingdefinedbuilt-inC++typeswehaveincludedthefollowing:
∙Tomakethisexamplemorecomplete,acopyconstructorhasbeenincludedalthoughthecompilerwillgenerateamember-wiseoneautomaticallyifneeded.Thishasthesamefunctionalityastheassignmentoperator(=).
∙Theassignment(=)operatormustbespecifiedsothatsortroutinescanassignanewordertothemembersofthelist.
∙The"
lessthan"
(<
)operatormustbespecifiedsothatsortroutinescandetermineifoneclassinstanceis"
another.
equalsto"
(==)operatormustbespecifiedsothatsortroutinescandetermineifoneclassinstanceis"
another.
//StandardTemplateLi