博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[STL][C++]LIST
阅读量:6888 次
发布时间:2019-06-27

本文共 3312 字,大约阅读时间需要 11 分钟。

参考:http://blog.csdn.net/whz_zb/article/details/6831817

list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素。在STL中,list和vector一样,是两个常被使用的容器。和vector不一样的是,list不支持对元素的任意存取。list中提供的成员函数与vector类似,不过list提供对表首元素的操作push_front、pop_front,这是vector不具备的。和vector另一点不同的是,list的迭代器不会存在失效的情况,他不像vector会保留备份空间,在超过容量额度时重新全部分配内存,导致迭代器失效;list没有备份空间的概念,出入一个元素就申请一个元素的空间,所以它的迭代器不会失效

 list每次增加一个元素,不存在重新申请内存的情况,它的成本是恒定的。而vector每当增加关键元素的时候,都需要重新申请新的更大的内存空间,会调用元素的自身的复制构造函数,存在构造成本。在销毁旧内存的时候,会调用析构函数,存在析构成本。所以在存储复杂类型和大量元素的情况下,list比vector更有优势! 

    List是一个双向链表,双链表既可以向前又向后链接他的元素。

    List将元素按顺序储存在链表中. 与 向量(vector)相比, 它允许快速的插入和删除,但是随机访问却比较慢。

 

List头文件:

  #include<list>

 

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中重复的元素

 

参考:http://blog.csdn.net/lskyne/article/details/10418823

#include 
#include
#include
#include
using namespace std; //创建一个list容器的实例LISTINT typedef list
LISTINT; //创建一个list容器的实例LISTCHAR typedef list
LISTCHAR; void main() { //用list容器处理整型数据 //用LISTINT创建一个名为listOne的list对象 LISTINT listOne; //声明i为迭代器 LISTINT::iterator i; //从前面向listOne容器中添加数据 listOne.push_front (2); listOne.push_front (1); //从后面向listOne容器中添加数据 listOne.push_back (3); listOne.push_back (4); //从前向后显示listOne中的数据 cout<<"listOne.begin()--- listOne.end():"<

结果:

listOne.begin()--- listOne.end():

1 2 3 4
listOne.rbegin()---listOne.rend():
4 3 2 1
Sum=10
------------------
listTwo.begin()---listTwo.end():
B A x y
The maximum element in listTwo is: y
Press any key to continue

 

#include 
#include
using namespace std; typedef list
INTLIST; //从前向后显示list队列的全部元素 void put_list(INTLIST list, char *name) { INTLIST::iterator plist; cout << "The contents of " << name << " : "; for(plist = list.begin(); plist != list.end(); plist++) cout << *plist << " "; cout<
list3: "<<(list1>list3)<

结果:

 

The contents of list1 :

The contents of list2 : 6 6 6 6 6 6 6 6 6 6
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1.push_back(2) and list1.push_back(4):
The contents of list1 : 2 4
list1.push_front(5) and list1.push_front(7):
The contents of list1 : 7 5 2 4
list1.insert(list1.begin()+1,3,9):
The contents of list1 : 7 9 9 9 5 2 4
list1.front()=7
list1.back()=4
list1.pop_front() and list1.pop_back():
The contents of list1 : 9 9 9 5 2
list1.erase(++list1.begin()):
The contents of list1 : 9 9 5 2
list2.assign(8,1):
The contents of list2 : 1 1 1 1 1 1 1 1
list1.max_size(): 1073741823
list1.size(): 4
list1.empty(): 0
The contents of list1 : 9 9 5 2
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1>list3: 1
list1<list3: 0
The contents of list1 : 2 5 9 9
The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9
The contents of list3 :
Press any key to continue

你可能感兴趣的文章
activemq搭建和使用
查看>>
java语言实现将mysql的linestring、point 数据类型解析成double类型
查看>>
TCP三次握手和四次挥手
查看>>
Nginx动态添加模块
查看>>
WPF和Silverlight概述(1)
查看>>
java笔记:第4章 流程控制
查看>>
《Python从小白到大牛》第8章 控制语句
查看>>
pxe自动安装系统 (linux)
查看>>
cacti关于1000M网卡的监控
查看>>
我的友情链接
查看>>
详解 Spotlight on Unix 监控Linux服务器
查看>>
docker 安装及简单配置
查看>>
关于大数据和古中国的一丝小想法随笔(续)
查看>>
十二年IT职业生涯心得--致我们终将逝去的青春(连载)
查看>>
我的友情链接
查看>>
初学Redis(2)——用Redis作为Mysql数据库的缓存
查看>>
使用Jersey构建图片服务器 有回显图片功能
查看>>
SQL 去重
查看>>
ubuntu 设置静态IP
查看>>
linux文档的压缩与打包
查看>>