List内部结构完全不同于array, vector, deque。

它提供了两个pointer,指向第一个和最后一个元素。

不支持随机访问元素,因此要访问第n个元素必须爬过n - 1个元素。

在任何位置上执行元素的插入和删除操作都很快。

因此会有一些属于list的特殊类型操作,比如merge, splice等。

ListTest.cpp

#include <iostream>
#include <deque>
#include <list>
#include <iostream>
#include <algorithm>
#include <iterator>
#include "ListTest.h" using namespace std; void ListTest::printLists(const list<int>& l1, const list<int>& l2)
{
cout << "list1: ";
copy(l1.cbegin(), l1.cend(), ostream_iterator<int>(cout, " "));
cout << endl << "list2: ";
copy(l2.cbegin(), l2.cend(), ostream_iterator<int>(cout, " "));
cout << endl << endl;
} void ListTest::simpleOperation()
{
// create two empty lists
list<int> list1, list2; // fill both lists with elements
for (int i = ; i<; ++i) {
list1.push_back(i);
list2.push_front(i);
}
printLists(list1, list2); // insert all elements of list1 before the first element with value 3 of list2
// - find() returns an iterator to the first element with value 3
list2.splice(find(list2.begin(), list2.end(), // destination position
),
list1); // source list
printLists(list1, list2); // move first element of list2 to the end
list2.splice(list2.end(), // destination position
list2, // source list
list2.begin()); // source position
printLists(list1, list2); // sort second list, assign to list1 and remove duplicates
list2.sort();
list1 = list2;
list2.unique();
printLists(list1, list2); // merge both sorted lists into the first list
list1.merge(list2);
printLists(list1, list2); // remove all even elements
list1.remove_if([](int i) {
return i % == ;
});
printLists(list1, list2);
} void ListTest::run()
{
printStart("simpleOperation()");
simpleOperation();
printEnd("simpleOperation()");
}

运行结果:

---------------- simpleOperation(): Run Start ----------------
list1: 0 1 2 3 4 5
list2: 5 4 3 2 1 0

list1:
list2: 5 4 0 1 2 3 4 5 3 2 1 0

list1:
list2: 4 0 1 2 3 4 5 3 2 1 0 5

list1: 0 0 1 1 2 2 3 3 4 4 5 5
list2: 0 1 2 3 4 5

list1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
list2:

list1: 1 1 1 3 3 3 5 5 5
list2:

---------------- simpleOperation(): Run End ----------------

STL - 容器 - List的更多相关文章

  1. STL容器

    啦啦啦,今天听啦高年级学长讲的STL容器啦,发现有好多东西还是有必要记载的,毕竟学长是身经百战的,他在参加各种比赛的时候积累的经验可不是一天两天就能学来的,那个可是炒鸡有价值的啊,啊啊啊啊啊 #inc ...

  2. c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

    c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...

  3. STL容器删除元素的陷阱

    今天看Scott Meyers大师的stl的用法,看到了我前段时间犯的一个错误,发现我写的代码和他提到错误代码几乎一模一样,有关stl容器删除元素的问题,错误的代码如下:std::vector< ...

  4. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  5. GDB打印STL容器内容

    GDB调试不能打印stl容器内容,下载此文件,将之保存为~/.gdbinit就可以使用打印命令了. 打印list用plist命令,打印vector用pvector,依此类推. (gdb) pvecto ...

  6. STL容器迭代器失效分析

    连续内存序列容器(vector, string, deque) 对于连续内存序列STL容器,例如vector,string,deque,删除当前iterator会使得后面所有的iterator都失效, ...

  7. STL容器的适用情况

     转自http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/ ly; mso-default-props:yes; m ...

  8. STL容器的遍历删除

    STL容器的遍历删除 今天在对截包程序的HashTable中加入计时机制时,碰到这个问题.对hash_map中的每个项加入时间后,用查询函数遍历hash_map,以删除掉那些在表存留时间比某个阈值长的 ...

  9. STL容器与配接器

    STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector                             行为类似于数组,但可以根据要求 ...

  10. STL容器的本质

    http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...

随机推荐

  1. 编写简单登陆和注册功能的demo时遇到的问题

    一.注册功能中添加数据不成功 给数据库添加EditText中的内容后,数据库中找不到添加后的数据,并且存在字符串为空的数据 解决方法:EditText registerAccount = (EditT ...

  2. ISO7816 (part 1-3) asynchronous smartcard information

    http://java.inf.elte.hu/java-1.3/javacard/iso7816.txt ============================================== ...

  3. oracle systemtap tracing

    https://github.com/LucaCanali?tab=repositories https://github.com/LucaCanali/Linux_tracing_scripts/t ...

  4. Eclipse配置mybatis-generator插件的2种方法

    原文:https://blog.csdn.net/u012825737/article/details/79117540 最近在做一个Mybatis的项目,学习到了一个插件mybatis-genera ...

  5. 配置Tomcat成为系统服务

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. RobotFramework自动化2-自定义关键字

    前言 有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象.Selenium2library提供了Get Webelements 关键字,用于定位一组元素 以百 ...

  7. [翻译] The Amazing Audio Engine

    The Amazing Audio Engine https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine The Amazing ...

  8. there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    建表语句: create table test_table(   id integer not null auto_increment primary key,   stamp_created tim ...

  9. OpenCV学习(18) 细化算法(6)

    本章我们在学习一下基于索引表的细化算法. 假设要处理的图像为二值图,前景值为1,背景值为0. 索引表细化算法使用下面的8邻域表示法: 一个像素的8邻域,我们可以用8位二进制表示,比如下面的8邻域,表示 ...

  10. 数学图形之SineSurface与粽子曲面

    SineSurface直译为正弦曲面.这有可能和你想象的正弦曲线不一样.如果把正弦曲线绕Y轴旋转,得到的该是正弦波曲面.这个曲面与上一节中的罗马曲面有些相似,那个是被捏过的正四面体,这个则是个被捏过正 ...