STL学习系列六:List容器
List简介
- list是一个双向链表容器,可高效地进行插入删除元素。
- list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符。it++(ok), it+5(err)
- #include <list>
1.list对象的默认构造
- list采用采用模板类实现,对象的默认构造形式:list<T> lstT; 如:
- list<int> lstInt; //定义一个存放int的list容器。
- list<float> lstFloat; //定义一个存放float的list容器。
- 尖括号内还可以设置指针类型或自定义类型。
2.list头尾的添加移除操作
- list.push_back(elem); //在容器尾部加入一个元素
- list.pop_back(); //删除容器中最后一个元素
- list.push_front(elem); //在容器开头插入一个元素
- list.pop_front(); //从容器开头移除第一个元素
- #include<iostream>
- using namespace std;
- #include <list>
- void objPlay2()
- {
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();//这个时候list就是(1,3,5,7,9)
- lstInt.pop_front(); //删除头元素(1)这个时候list就是(3,5,7,9)
- lstInt.pop_front(); //删除头元素(3)这个时候list就是(5,7,9)
- lstInt.push_front();//头部插入11 这个时候list就是(11,5,7,9)
- lstInt.push_front();//头部插入13 这个时候list就是(13,11,5,7,9)
- lstInt.pop_back();//删除尾元素 这个时候list就是(13,11,5,7)
- lstInt.pop_back();//删除尾元素 这个时候list就是(13,11,5,)
- }
- int main()
- {
- objPlay2();
- return ;
- }
3.list的数据存取
- list.front(); //返回第一个元素。
- list.back(); //返回最后一个元素。
- void objPlay3()
- {
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();//这个时候list就是(1,3,5,7,9)
- int iFront = lstInt.front(); //取到list的头部(1)
- int iBack = lstInt.back(); //取list的尾部(9)
- lstInt.front() = ; //给list的头部设置值,覆盖原来的值
- lstInt.back() = ; //给list的尾部设置值,覆盖原来的值
- }
4.list与迭代器
- list.begin(); //返回容器中第一个元素的迭代器。
- list.end(); //返回容器中最后一个元素之后的迭代器。
- list.rbegin(); //返回容器中倒数第一个元素的迭代器。
- list.rend(); //返回容器中倒数最后一个元素的后面的迭代器。
- void objPlay4()
- {
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); ++it)//正向迭代器
- {
- cout << *it << "\t";
- }
- cout << endl;
- for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); ++rit)//反向迭代器
- {
- cout << *rit << "\t";
- }
- }
5.list对象的带参数构造
- list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。
- list(n,elem); //构造函数将n个elem拷贝给本身。
- list(const list &lst); //拷贝构造函数。
- void objPlay5()
- {
- list<int> lstIntA;
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- list<int> lstIntB(lstIntA.begin(), lstIntA.end()); //1 3 5 7 9
- list<int> lstIntC(, ); //8 8 8 8 8
- list<int> lstIntD(lstIntA); //1 3 5 7 9
- }
6.list的赋值
- list.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。
- list.assign(n,elem); //将n个elem拷贝赋值给本身。
- list& operator=(const list &lst); //重载等号操作符
- list.swap(lst); // 将lst与本身的元素互换。
- void objPlay6()
- {
- list<int> lstIntA, lstIntB, lstIntC, lstIntD;//这个地方时先定义了list,注意和有参构造的区别
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntB.assign(lstIntA.begin(), lstIntA.end()); //1 3 5 7 9
- lstIntC.assign(, ); //8 8 8 8 8
- lstIntD = lstIntA; //1 3 5 7 9
- lstIntC.swap(lstIntD); //互换
- }
7.list的大小
- list.size(); //返回容器中元素的个数
- list.empty(); //判断容器是否为空
- list.resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
- list.resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
- void objPlay7()
- {
- list<int> lstIntA;
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- if (!lstIntA.empty())
- {
- int iSize = lstIntA.size(); //
- lstIntA.resize(); //1 3 5 0 0
- lstIntA.resize(, ); //1 3 5 0 0 1 1
- lstIntA.resize(); //1 3
- }
- }
8.list的插入
- list.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
- list.insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
- list.insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。
- void objPlay8()
- {
- list<int> lstA;
- list<int> lstB;
- lstA.push_back();
- lstA.push_back();
- lstA.push_back();
- lstA.push_back();
- lstA.push_back();
- lstB.push_back();
- lstB.push_back();
- lstB.push_back();
- lstB.push_back();
- lstA.insert(lstA.begin(), ); //{11, 1, 3, 5, 7, 9}
- lstA.insert(++lstA.begin(), , ); //{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}
- }
9.list的删除
- list.clear(); //移除容器的所有数据
- list.erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。注意是左闭右开
- list.erase(pos); //删除pos位置的数据,返回下一个数据的位置。
- lst.remove(elem); //删除容器中所有与elem值匹配的元素。
- void objPlay9()
- {
- //1.删除区间内的元素
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- list<int>::iterator itBegin = lstInt.begin();
- ++itBegin;//begin指向3
- list<int>::iterator itEnd = lstInt.begin();
- ++itEnd;
- ++itEnd;
- ++itEnd;//end指向7
- lstInt.erase(itBegin, itEnd);
- //此时容器lstInt包含按顺序的1,7,9三个元素。
- int arr[] = { , , , , , , , , , };
- for (list<int>::iterator it = lstInt.begin(); it != lstInt.end();) //小括号里不需写 ++it
- {
- if (*it == )
- {
- it = lstInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。//此时,不执行 ++it;
- }
- else
- {
- ++it;
- }
- }
- //2.删除容器中等于3的元素的方法二
- lstInt.remove();
- //3.删除lstInt的所有元素
- lstInt.clear(); //容器为空
- }
10.list的反序排列
- lst.reverse(); //反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
- void objPlay10()
- {
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.reverse(); //9 7 5 3 1
- }
以上所有代码整理:
- #include<iostream>
- using namespace std;
- #include <list>
- void objPlay2()
- {
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();//这个时候list就是(1,3,5,7,9)
- lstInt.pop_front(); //删除头元素(1)这个时候list就是(3,5,7,9)
- lstInt.pop_front(); //删除头元素(3)这个时候list就是(5,7,9)
- lstInt.push_front();//头部插入11 这个时候list就是(11,5,7,9)
- lstInt.push_front();//头部插入13 这个时候list就是(13,11,5,7,9)
- lstInt.pop_back();//删除尾元素 这个时候list就是(13,11,5,7)
- lstInt.pop_back();//删除尾元素 这个时候list就是(13,11,5,)
- }
- void objPlay3()
- {
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();//这个时候list就是(1,3,5,7,9)
- int iFront = lstInt.front(); //取到list的头部(1)
- int iBack = lstInt.back(); //取list的尾部(9)
- lstInt.front() = ; //给list的头部设置值,覆盖原来的值
- lstInt.back() = ; //给list的尾部设置值,覆盖原来的值
- }
- void objPlay4()
- {
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); ++it)//正向迭代器
- {
- cout << *it << "\t";
- }
- cout << endl;
- for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); ++rit)//反向迭代器
- {
- cout << *rit << "\t";
- }
- }
- void objPlay5()
- {
- list<int> lstIntA;
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- list<int> lstIntB(lstIntA.begin(), lstIntA.end()); //1 3 5 7 9
- list<int> lstIntC(, ); //8 8 8 8 8
- list<int> lstIntD(lstIntA); //1 3 5 7 9
- }
- void objPlay6()
- {
- list<int> lstIntA, lstIntB, lstIntC, lstIntD;//这个地方时先定义了list,注意和有参构造的区别
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntB.assign(lstIntA.begin(), lstIntA.end()); //1 3 5 7 9
- lstIntC.assign(, ); //8 8 8 8 8
- lstIntD = lstIntA; //1 3 5 7 9
- lstIntC.swap(lstIntD); //互换
- }
- void objPlay7()
- {
- list<int> lstIntA;
- lstIntA.push_back();
- lstIntA.push_back();
- lstIntA.push_back();
- if (!lstIntA.empty())
- {
- int iSize = lstIntA.size(); //
- lstIntA.resize(); //1 3 5 0 0
- lstIntA.resize(, ); //1 3 5 0 0 1 1
- lstIntA.resize(); //1 3
- }
- }
- void objPlay8()
- {
- list<int> lstA;
- list<int> lstB;
- lstA.push_back();
- lstA.push_back();
- lstA.push_back();
- lstA.push_back();
- lstA.push_back();
- lstB.push_back();
- lstB.push_back();
- lstB.push_back();
- lstB.push_back();
- lstA.insert(lstA.begin(), ); //{11, 1, 3, 5, 7, 9}
- lstA.insert(++lstA.begin(), , ); //{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}
- }
- void objPlay9()
- {
- //1.删除区间内的元素
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- list<int>::iterator itBegin = lstInt.begin();
- ++itBegin;//begin指向3
- list<int>::iterator itEnd = lstInt.begin();
- ++itEnd;
- ++itEnd;
- ++itEnd;//end指向7
- lstInt.erase(itBegin, itEnd);
- //此时容器lstInt包含按顺序的1,7,9三个元素。
- int arr[] = { , , , , , , , , , };
- for (list<int>::iterator it = lstInt.begin(); it != lstInt.end();) //小括号里不需写 ++it
- {
- if (*it == )
- {
- it = lstInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。//此时,不执行 ++it;
- }
- else
- {
- ++it;
- }
- }
- //2.删除容器中等于3的元素的方法二
- lstInt.remove();
- //3.删除lstInt的所有元素
- lstInt.clear(); //容器为空
- }
- void objPlay10()
- {
- list<int> lstInt;
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.push_back();
- lstInt.reverse(); //9 7 5 3 1
- }
- int main()
- {
- objPlay2();
- objPlay3();
- objPlay4();
- objPlay5();
- objPlay6();
- objPlay7();
- objPlay8();
- objPlay9();
- objPlay10();
- system("pause");
- return ;
- }
STL学习系列六:List容器的更多相关文章
- 标准模板库(STL)学习探究之vector容器
标准模板库(STL)学习探究之vector容器 C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...
- 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)
layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ...
- STL学习系列之一——标准模板库STL介绍
库是一系列程序组件的集合,他们可以在不同的程序中重复使用.C++语言按照传统的习惯,提供了由各种各样的函数组成的库,用于完成诸如输入/输出.数学计算等功能. 1. STL介绍 标准模板库STL是当今每 ...
- STL学习系列七:优先级队列priority_queue容器
1.简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL priority_queue 用来开发一些特殊的应用,请对stl的类库,多做扩展性学习 这里给个例子: #include< ...
- STL学习系列三:Deque容器
1.Deque简介 deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. deque在接口上和vector非常 ...
- STL学习系列二:Vector容器
1.Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添 ...
- STL学习系列四:Stack容器
Stack简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. #include <stack> 1.stack对象的默认构造 ...
- STL学习系列九:Map和multimap容器
1.map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...
- STL学习系列八:Set和multiset容器
1.set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实 ...
随机推荐
- tornado中使用torndb,连接数过高的问题
问题背景 最近新的产品开发中,使用了到了Tornado和mysql数据库.但在基本框架完成之后,我在开发时候发现了一个很奇怪的现象,我在测试时,发现数据库返回不了结果,于是我在mysql中输入show ...
- IT玄幻小说
职业 设计:菜鸟 美工<初级,中级,高级,资深> 设计师<初级,中级,高级,资深> 大神 前端:菜鸟 前端<初级,中级,高级,资深> 架构师<初级,中 ...
- 事务报错 [Exception] 当前 TransactionScope 已完成
捕获异常的时候 偶尔会碰到这个异常报告 导致原因 //正确代码 using (TransactionScope ts = new TransactionScope(TransactionScopeOp ...
- [总结]FFMPEG视音频编解码零基础学习方法--转
ffmpeg编解码学习 目录(?)[-] ffmpeg程序的使用ffmpegexeffplayexeffprobeexe 1 ffmpegexe 2 ffplayexe 3 ffprobeexe ...
- linux lnmp编译安装
关闭SELINUX vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled ...
- liunx上运行mybase
mybase 是个人知识管理软件,国内用他的人很多,尤其是程序员.我也是mybase的忠实使用者,有大量的积累. 以前一直用Windows,mybase使用完全没有问题,后来转投ubuntu阵营 ...
- Wireshark和TcpDump抓包分析心得
Wireshark和 TcpDump抓包分析心得 1. Wireshark与tcpdump介绍 Wireshark是一个网络协议检测工具,支持Windows平台和Unix平台,我一般只在Window ...
- .NET Remoting
.NET Remoting .NET Remoting是微软早期的分布式通信技术,虽然微软后来通过WCF通用基础通信框架整合掉了,但是通过回顾学习Remoting,反过来学习理解WCF也是很有帮助 ...
- spoj 694(后缀数组)
题意:求一个字符串的不重复子串的个数. 分析:对于下标为i的位置,能够产生的前缀子串个数为len-i(下标从0开始),对于与它字典序相邻的后缀产生的子串是重复的(就是他们的最长公共前缀),所以我们要减 ...
- A标记点击后去掉虚线
没加时,谷歌无虚线,但ie有虚线. 加上这句,所有的a标签点击都没有虚线了. a {outline: none;} a:active {star:expression_r(this.onFocus=t ...