双向循环链表listWord格式.docx

上传人:b****0 文档编号:13647120 上传时间:2022-10-12 格式:DOCX 页数:9 大小:73.39KB
下载 相关 举报
双向循环链表listWord格式.docx_第1页
第1页 / 共9页
双向循环链表listWord格式.docx_第2页
第2页 / 共9页
双向循环链表listWord格式.docx_第3页
第3页 / 共9页
双向循环链表listWord格式.docx_第4页
第4页 / 共9页
双向循环链表listWord格式.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

双向循环链表listWord格式.docx

《双向循环链表listWord格式.docx》由会员分享,可在线阅读,更多相关《双向循环链表listWord格式.docx(9页珍藏版)》请在冰豆网上搜索。

双向循环链表listWord格式.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1