链表c语言stl,C++STL之List容器

1.再谈链表

List链表的概念再度出现了,作为线性表的一员,C++的STL提供了快速进行构建的方法,为此,在前文的基础上通过STL进行直接使用,这对于程序设计中快速构建原型是相当有必要的,这里的STL链表是单链表的形式。

2.头文件

头文件:#include

3.初始化

格式为:explicit list (const allocator_type& alloc = allocator_type());

我们以int类型作为参数为例进行创建,其创建方法与vector无异list l1;           //创建一个空链表

list l2(10);       //创建一个链表其有10个空元素

list l3(5,20);     //创建一个链表其有5个元素内容为20

list l4(l3.begin(),l3.end());  //创建一个链表其内容为l3的内容

list l5(l4);               //创建一个链表其内容为l4的内容

4. 迭代器

遍历代码举例(其方法和vector版本无异只是更加精简):list li;

for(list::iterator it=li.begin();it!=li.end();it++){

cout<

}

5. 常用接口

我们使用list li;预先创建了一个链表,命名为li,方便举例

a)判断是否为空empty()

返回一个bool类型的值,只存在真和假,当链表为空时为真,不为空时为假

函数原型

bool empty() const;if(li.empty()){     //当链表为空的时候执行

cout<

}else{

cout<

}

b)获取大小size()

返回链表元素的个数

函数原型

size_type size() const;cout<

c) 链表前插入push_front() &&删除 pop_front()

push_front()表示在链表最前端插入一个数据,pop_front()表示在链表最前端删除一个数据。

函数原型

void push_front (const value_type& val);

void pop_front();li.push_front(10);

li.pop_front();

d) 链表后插入push_back() &&删除 pop_back()

push_back()表示在链表尾插入一个数据,pop_back()表示将链表尾删除一个数据。

函数原型:

void push_back (const value_type& val);

void pop_back();li.push_back(10);

li.pop_back();

e) 插入insert()

插入元素到指定位置,通过在元素之前在指定位置插入新元素来扩展向量,从而有效地增加容器大小所插入的元素数量。

函数原型:

插入单一数据到指定位置:

iterator insert (iterator position, const value_type& val);

插入一段数据到指定位置:

void insert (iterator position, size_type n, const value_type& val);

插入一段别的容器的数据到指定位置:

template

void insert (iterator position, InputIterator first, InputIterator last);

使用举例:li.insert(li.begin(),10);     //在链表最前端插入数据10

li.insert(li.begin(),5,20);   //在链表最前端插入5个数据内容为20

list k(2,50);   //创建一个新的链表k,其拥有2个元素内容均为50

li.insert(li.begin(),li.begin(),li.end());  //在链表v最前端插入链表上K的全部内容

f) 删除erase()

删除一个元素,或者是一段区间的元素,将会自动缩减空间使用。

函数原型:

iterator erase (iterator position);

iterator erase (iterator first, iterator last);

使用举例li.erase(li.begin());     //删除第一个元素

li.erase(li.begin(),li.begin()+4); //删除前4个元素

g)排序sort()

让整个链表变成升序状态,或者变成自定义的排序状态

函数原型:

void sort();

template    void sort (Compare comp);

详细举例:#include

#include

using namespace std;s

int cmp(const int &a,const int &b){

//简单的自定义降序序列

return a>b;

}

int main(){

list li;           //创建一个空链表

for(int i=10;i>=6;i--){

li.push_back(i);

}

li.push_front(3);

li.push_back(20);

list li2(li);

for(list::iterator it=li.begin();it!=li.end();it++){

cout<

}

cout<

//排序前3 10 9 8 7 6 20//

li.sort();

for(list::iterator it=li.begin();it!=li.end();it++){

cout<

}

cout<

//默认排序后 3 6 7 8 9 10 20//

li2.sort(cmp);

for(list::iterator it=li2.begin();it!=li2.end();it++){

cout<

}

cout<

//自定义排序后 20 10 9 8 7 6 3//

return 0;

}

h)逆序reverse()

相对于自定义的降序方法,STL提供了一个默认的降序方法reverse(),类似于sort一样直接使用即可。

void reverse();li.reverse();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440990.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

*【牛客 - 318B】签到题(单调栈,水题)

题干&#xff1a; 众所周知&#xff0c;IG是英雄联盟S8世界总决赛冠军&#xff0c;夺冠之夜&#xff0c;数亿人为之欢呼&#xff01; 赛后某百分百胜率退役ADC选手的某表情包意外走红&#xff0c;某苟会长看到此表情包也想模仿。 于是有n个友爱的萌新决定每人都送会长一根长…

c 语言车牌识别系统课题设计,车牌识别系统的设计--课程设计报告.doc

车牌识别系统的设计--课程设计报告目录一、摘要:3二、设计目的和意义:32.1、设计目的&#xff1a;32.2、设计意义&#xff1a;3三、设计原理:3四、详细设计步骤:34.1、提出总体设计方案:44.2、各模块的实现:5五、设计结果及分析20六、总结:22七、体会23八、参考文献:23一、摘要…

*【HDU - 2586】How far away ? (LCA模板题,倍增)

题干&#xff1a; There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int th…

android volley 上传图片 和参数,Android使用Volley上传文件

一个项目中用到的使用Volley上传头像文件的例子/*** Created by wangshihui on 2015/11/30.* 上传文件* url&#xff1a;.....method&#xff1a;post参数&#xff1a;file接口给的参数&#xff1a;file 就是表单的key&#xff0c;传给mFilePartName;这是个测试类&#xff0c;…

【HDU - 4056】Draw a Mess (并查集 or 线段树)

题干&#xff1a; Its graduated season, every students should leave something on the wall, so....they draw a lot of geometry shape with different color. When teacher come to see what happened, without getting angry, he was surprised by the talented achiev…

android 按钮按下缩放,android捏缩放

我TextView使用本教程为我实现了一个缩放缩放。结果代码是这样的&#xff1a;private GestureDetector gestureDetector;private View.OnTouchListener gestureListener;并在onCreate()中&#xff1a;// Zoom handlersgestureDetector new GestureDetector(new MyGestureDetec…

【CodeForces - 520B】Two Buttons (bfs或dp或时光倒流,trick)

题干&#xff1a; Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After click…

android代码旋转屏幕,Android Activity源码分析--windowmanager屏幕旋转研究

注意&#xff1a;鄙人看的是6.0的代码Activity里面还是调用了WindowManager来显示界面。在activity的738行&#xff0c;有这几行代码private Window mWindow;private WindowManager mWindowManager;/*package*/ View mDecor null; //这就是activity的主view&#xff0c;我也不…

android surfaceflinger 代码,android surfaceflinger测试程序

frameworks/base/libs/surfaceflinger/tests/resize/resize.cpp 是个好地方。 但是我的测试应用程序版本( 来自供应商的Eclair ) 过时了&#xff0c;有些 Surface API已经转移到 SurfaceControl&#xff0c;你必须&#xff1a;SurfaceComposerClient::createSurface() > Sur…

【牛客 - 315B】 勇气获得机(二叉树性质,思维,知识点,tricks)

题干&#xff1a; 妞妞听说Nowcoder Girl女生编程挑战赛要开始了, 但是她没有足够的勇气报名参加, 牛牛为了帮助妞妞,给她准备一台勇气获得机。初始的时候妞妞的勇气值是0, 勇气获得机有两个按钮: 1、N按钮: 如果当期拥有的勇气值为x, 按下之后勇气值将变为2*x1&#xff0c…

【CodeForces - 357C 】Knight Tournament(并查集 或 STLset)

题干&#xff1a; Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event. As for you, youre just a simple peasant. …

qt android wifi,QtScrcpy: Android实时投屏软件,此应用程序提供USB(或通过TCP/IP)连接的Android设备的显示和控制。它不需要任何root访问权限...

QtScrcpyQtScrcpy可以通过USB(或通过TCP/IP)连接Android设备&#xff0c;并进行显示和控制。不需要root权限。单个应用程序最多支持16个安卓设备同时连接。同时支持GNU/Linux&#xff0c;Windows和MacOS三大主流桌面平台它专注于:精致 (仅显示设备屏幕)性能 (30~60fps)质量 (19…

android 添加so,Android studio 中添加 .so 文件

场景&#xff1a;Android studio 编译我的项目(项目中有运用的jni)&#xff0c;编译没有报错&#xff0c;正常的安装到我的机器上&#xff0c;可是运行的时候就报错&#xff0c;没有找到*.so文件...可是明明在libs&#xff0c;目录下有加相关的文件&#xff1f;参考网上大部分的…

【UVA - 10154 】Weights and Measures (贪心排序,dp,类似0-1背包,状态设定思维)

题干&#xff1a; The Problem Mack, in an effort to avoid being cracked, has enlisted your advice as to the order in which turtles should be dispatched to form Yertles throne. Each of the five thousand, six hundred and seven turtles ordered by Yertle has …

投票抵制华为鸿蒙系统,网友投票华为十大技术:鸿蒙OS仅排第二!

作为国内消费电子巨头&#xff0c;华为的技术实力是有目共睹的&#xff0c;在过去的一年发布的许多黑科技让人眼前一亮&#xff0c;那么今日(17日)消息&#xff0c;华为终端今天表示&#xff0c;此前向粉丝们征集票选过去这一年里大家最关注的十大功能技术。最终&#xff0c;收…

【牛客 - 318L】彪神666(水题,半高精度,递推,trick)

题干&#xff1a; 在国外&#xff0c;666代表魔鬼&#xff0c;777代表上帝。 所以牛逼的彪神就非常不喜欢6这个数字。 有一天彪神突发奇想&#xff0c;&#xff0c;他想求一些书与6无关的数。 如果一个数能被6整除&#xff0c;或者它的十进制表示法中某位上的数字为6&…

平板android怎么玩电脑游戏,Android平板模拟家用主机游戏教程_小米 平板_平板电脑新闻-中关村在线...

一、NESoid看完了上一页Windows系统模拟器介绍的网友应该能得出一个经验&#xff0c;一般模拟器的名称都和其模拟的游戏主机名称比较类似&#xff0c;所以很多模拟器都可以通过其名称判断出它到底是模拟谁的。比如这款NESoid&#xff0c;看名字就知道是模拟NES主机&#xff0c;…

【牛客 - 297B】little w and Sum(水题,前缀和)

题干&#xff1a; 小w与tokitsukaze一起玩3ds上的小游戏&#xff0c;现在他们遇到了难关。 他们得到了一个数列&#xff0c;通关要求为这个数列的和为0&#xff0c;并且只有一次改变一个数的符号的机会(正数变成负数&#xff0c;负数变成正数)。 请问小w与tokitsukaze能否…

华为nova 7 se鸿蒙,荣耀v40和华为Nova7Pro哪个好-参数对比-更值得入手

荣耀v40已经发布&#xff0c;今天小编给大家带来荣耀v40和华为Nova7Pro参数详细分析&#xff0c;这两款手机有什么区别&#xff0c;哪一个更加值得入手呢&#xff0c;一起来看看吧一、参数对比迷你手机网荣耀v40​华为Nova7Pro手机外形屏幕尺寸6.72英寸6.57屏幕材质OLEDOLED刷新…

【HDU - 1102】Constructing Roads (最小生成树裸题模板)

题干&#xff1a; There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or the…