List简介

list是一个双向链表容器,可高效地进行插入删除元素。

list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符。It++(ok) it+5(err)

#include <list>

list对象的默认构造

list采用采用模板类实现,对象的默认构造形式:list<T> lstT; 如:

list<int> lstInt; //定义一个存放int的list容器。

list<float> lstFloat; //定义一个存放float的list容器。

list<string> lstString; //定义一个存放string的list容器。

...

//尖括号内还可以设置指针类型或自定义类型。

list头尾的添加移除操作
	list.push_back(elem);	   //在容器尾部加入一个元素
	list.pop_back();              //删除容器中最后一个元素
	list.push_front(elem);     //在容器开头插入一个元素
	list.pop_front();              //从容器开头移除第一个元素

	list<int> lstInt;
	lstInt.push_back(1);
	lstInt.push_back(3);
	lstInt.push_back(5);
	lstInt.push_back(7);
	lstInt.push_back(9);
	lstInt.pop_front();
	lstInt.pop_front();
	lstInt.push_front(11);
	lstInt.push_front(13);
	lstInt.pop_back();
	lstInt.pop_back();
// lstInt    {13,11,5}
list的数据存取
	list.front();   //返回第一个元素。
	list.back();  //返回最后一个元素。

list<int> lstInt;
	lstInt.push_back(1);
	lstInt.push_back(3);
	lstInt.push_back(5);
	lstInt.push_back(7);
	lstInt.push_back(9);

	int iFront = lstInt.front();	//1
	int iBack = lstInt.back();		//9
	lstInt.front() = 11;			//11
	lstInt.back() = 19;			//19
list与迭代器
	list.begin();                     //返回容器中第一个元素的迭代器。
	list.end();                       //返回容器中最后一个元素之后的迭代器。
	list.rbegin();         //返回容器中倒数第一个元素的迭代器。
	list.rend();         //返回容器中倒数最后一个元素的后面的迭代器。

	list<int> lstInt;
	lstInt.push_back(1);
	lstInt.push_back(3);
	lstInt.push_back(5);
	lstInt.push_back(7);
	lstInt.push_back(9);

	for (list<int>::iterator it=lstInt.begin(); it!=lstInt.end(); ++it)
	{
		cout << *it;
		cout << " ";
	}

	for (list<int>::reverse_iterator rit=lstInt.rbegin(); rit!=lstInt.rend(); ++rit)
	{
		cout << *rit;
		cout << " ";
	}
list对象的带参数构造
	list(beg,end);    //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。
	list(n,elem);   //构造函数将n个elem拷贝给本身。
	list(const list &lst);  //拷贝构造函数。

list<int> lstIntA;
	lstIntA.push_back(1);
	lstIntA.push_back(3);
	lstIntA.push_back(5);
	lstIntA.push_back(7);
	lstIntA.push_back(9);

	list<int> lstIntB(lstIntA.begin(),lstIntA.end());		//1 3 5 7 9
	list<int> lstIntC(5,8);							//8 8 8 8 8
	list<int> lstIntD(lstIntA);						//1 3 5 7 9
list的赋值
	list.assign(beg,end);    //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。
	list.assign(n,elem);  //将n个elem拷贝赋值给本身。
	list& operator=(const list &lst);	//重载等号操作符
	list.swap(lst);  // 将lst与本身的元素互换。

	list<int> lstIntA,lstIntB,lstIntC,lstIntD;
	lstIntA.push_back(1);
	lstIntA.push_back(3);
	lstIntA.push_back(5);
	lstIntA.push_back(7);
	lstIntA.push_back(9);

	lstIntB.assign(lstIntA.begin(),lstIntA.end());		//1 3 5 7 9
	lstIntC.assign(5,8);							//8 8 8 8 8
	lstIntD = lstIntA;							//1 3 5 7 9
	lstIntC.swap(lstIntD);						//互换
list的大小
	list.size();	   //返回容器中元素的个数
	list.empty();	   //判断容器是否为空
	list.resize(num);   //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
	list.resize(num, elem);  //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

	list<int> lstIntA;
	lstIntA.push_back(1);
	lstIntA.push_back(3);
	lstIntA.push_back(5);

	if (!lstIntA.empty())
	{
		int iSize = lstIntA.size();		//3
		lstIntA.resize(5);			//1 3 5 0 0
		lstIntA.resize(7,1);			//1 3 5 0 0 1 1
		lstIntA.resize(2);			//1 3
	}
list的插入
	list.insert(pos,elem);   //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
	list.insert(pos,n,elem);   //在pos位置插入n个elem数据,无返回值。
	list.insert(pos,beg,end);   //在pos位置插入[beg,end)区间的数据,无返回值。

	list<int> lstA;
	list<int> lstB;

	lstA.push_back(1);
	lstA.push_back(3);
	lstA.push_back(5);
	lstA.push_back(7);
	lstA.push_back(9);

	lstB.push_back(2);
	lstB.push_back(4);
	lstB.push_back(6);
	lstB.push_back(8);

	lstA.insert(lstA.begin(), 11);		//{11, 1, 3, 5, 7, 9}
	lstA.insert(++lstA.begin(),2,33);		//{11,33,33,1,3,5,7,9}
	lstA.insert(lstA.begin() , lstB.begin() , lstB.end() );	//{2,4,6,8,11,33,33,1,3,5,7,9}
list的删除
	list.clear();		//移除容器的所有数据
	list.erase(beg,end);  //删除[beg,end)区间的数据,返回下一个数据的位置。
	list.erase(pos);    //删除pos位置的数据,返回下一个数据的位置。
	lst.remove(elem);   //删除容器中所有与elem值匹配的元素。

删除区间内的元素
lstInt是用list<int>声明的容器,现已包含按顺序的1,3,5,6,9元素。
list<int>::iterator itBegin=lstInt.begin();
++ itBegin;
list<int>::iterator itEnd=lstInt.begin();
++ itEnd;
++ itEnd;
++ itEnd;
lstInt.erase(itBegin,itEnd);
//此时容器lstInt包含按顺序的1,6,9三个元素。

假设 lstInt 包含1,3,2,3,3,3,4,3,5,3,删除容器中等于3的元素的方法一
for(list<int>::iterator it=lstInt.being(); it!=lstInt.end(); )    //小括号里不需写  ++it
{
   if(*it == 3)
   {
        it  =  lstInt.erase(it);       //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。
         //此时,不执行  ++it;
   }
   else
   {
       ++it;
   }
}

删除容器中等于3的元素的方法二
lstInt.remove(3);

删除lstInt的所有元素
lstInt.clear();			//容器为空
list的反序排列
	lst.reverse();     //反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。

	list<int> lstA;

	lstA.push_back(1);
	lstA.push_back(3);
	lstA.push_back(5);
	lstA.push_back(7);
	lstA.push_back(9);

	lstA.reverse();			//9 7 5 3 1

demo

#include <iostream>
#include <cstdio>
#include <list>
#include <algorithm>

using namespace std;

void printList(list<int> &l)
{
	for (list<int>::iterator it = l.begin(); it != l.end(); ++it) {
		cout << *it << ' ';
	}
	cout << endl;
}

void listInit()
{
	list<int> l;
	cout << "size of l: " << l.size() << endl;
	// size of l: 0

	for (int i = 0; i < 10; ++i) {
		l.push_back(i); // 尾插法
	}
	cout << "size of l: " << l.size() << endl;
	// size of l: 10

	printList(l);
	// 0 1 2 3 4 5 6 7 8 9

	// list不能随便访问
	list<int>::iterator it = l.begin();
	++it;
	++it;
	++it;
	//it = it + 5;// 不支持随机的访问容器
	l.insert(it, 100); // 插入到3的前面
	printList(l);
	// 0 1 2 100 3 4 5 6 7 8 9

	// 结论1:链表的结点index序号是从0号位置开始的
	// 在3号位置插入元素,让原来的3号位置变成4号位置,原来的4号位置变成5号位置

}

void listDelete()
{
	list<int> l;

	for (int i = 0; i < 10; ++i) {
		l.push_back(i); // 尾插法
	}
	printList(l);
	// 0 1 2 3 4 5 6 7 8 9
	list<int>::iterator it1 = l.begin();
	list<int>::iterator it2 = l.begin();
	++it2;
	++it2;
	++it2;
	l.erase(it1, it2);
	printList(l);
	// 3 4 5 6 7 8 9

	l.insert(l.begin(), 100);
	l.insert(l.begin(), 100);
	l.insert(l.begin(), 100);
	printList(l);
	// 100 100 100 3 4 5 6 7 8 9

	l.erase(l.begin());
	printList(l);
	//100 100 3 4 5 6 7 8 9

	l.remove(100);
	printList(l);
	// 3 4 5 6 7 8 9
}

int main()
{
	listInit();
	listDelete();

	return 0;
}

STL - list(双向链表)的更多相关文章

  1. C++ STL 关于双向链表list的splice函数

    转载自https://blog.csdn.net/qjh5606/article/details/85881680 list::splice实现list拼接的功能.将源list的内容部分或全部元素删除 ...

  2. STL容器:list双向链表学习

    list是一个双向列表容器,完成了标准C++数据结构中链表的所有功能; list与vector和deque类似,只不过其中的对象提供了对元素的随机访问. STL以双向链表的方式实现list,访问需要从 ...

  3. 码海拾遗:基于MySQL Connector/C++的MySQL操作(连接池)

    1.MySQL安装及简单设置 (1)安装:在OSX系统下,可以使用万能的“brew install”命令来进行安装:brew isntall mysql(默认安装最新版的MySQL) (2)启动:br ...

  4. STL之list(双向链表)

    一,概述 List将元素按顺序储存在链表中. 与向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. list 不仅是一个双向链表,而且是一个环状双向链表 二,使用 #incl ...

  5. C++进阶 STL(2) 第二天 一元/二元函数对象、一元/二元谓词、stack容器、queue容器、list容器(双向链表)、set容器、对组、map容器

    01 上次课程回顾 昨天讲了三个容器 string  string是对char*进行的封装 vector 单口容器 动态数组 deque(双端队列) 函数对象/谓词: 一元函数对象: for_each ...

  6. STL迭代器的使用、正向、逆向输出双向链表中的所有元素

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  7. stl之list双向链表容器应用基础

    不同于採用线性表顺序存储结构的vector和deque容器.list双向链表中任一位置的元素差值.插入和删除,都具有高效的常数阶算法时间复杂度O(1). 头文件 #include<list> ...

  8. ZOJ - 4016 Mergeable Stack (STL 双向链表)

    [传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) ...

  9. C++ STL简述

    前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...

随机推荐

  1. oh forever love~

    npm install -g forever forever start c9sdk/server.js --listen 0.0.0.0 --port 80 -a aa:111 -w ~ To el ...

  2. Android5.0特性阴影效果和裁剪

    阴影和剪裁 View的z属性 Material Design建议为了凸显布局的层次,建议使用阴影效果,并且Android L为了简化大家的工作,对View进行了扩展,能使大家非常方便的创建阴影效果: ...

  3. 将meteor部署到自己的服务器(deploy meteor to your own server)

    安装指定版本的node # 所有版本在:https://nodejs.org/download/release/# current dir:/rootwget -c https://nodejs.or ...

  4. Android 玩转IOC,Retfotit源码解析,教你徒手实现自定义的Retrofit框架

    CSDN:码小白 原文地址: http://blog.csdn.net/sk719887916/article/details/51957819 前言 Retrofit用法和介绍的文章实在是多的数不清 ...

  5. Solr 5.5.0 + tomcat 7.0.69 + zookeeper-3.4.6 Cloud部署

    Solr介绍:Solr是一个独立的企业级搜索应用服务器,Solr基于Lucene的全文搜索服务器,同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展并对查询性能进行了 ...

  6. 3.2、Android Studio在物理设备中运行APP

    当你构建一个Android应用时,在发布给用户之前,在物理设备上测试一下你的应用是非常必要的. 你可以使用Android设备作为运行.调试和测试应用的环境.包含在SDK中的工具让你在编译完成后在设备中 ...

  7. (一二三)基于GCD的dispatch_once实现单例设计

    要实现单例,关键是要保证类的alloc和init只被调用一次,并且被自身强引用防止释放. 近日读唐巧先生的<iOS开发进阶>,受益匪浅,通过GCD实现单例就是收获之一,下面把这个方法与大家 ...

  8. javascript 下拉列表 自动取值 无需value

    <select id="applyType" name="$!{status.expression}" class="inp" onc ...

  9. Android初级教程理论知识(第五章页面跳转和数据传递)

    总体概述: Android四大组件 Activity BroadCastReceiver Service ContentProvider 创建第二个activity 新创建的activity,必须在清 ...

  10. android notification,notificationmanager详解

    我们知道在使用Android的通知的时候一定会用到NotificationManager . Notification这两个类,这两个类的作用分别是: NotificationManager :  是 ...