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. CF1060C Maximum Subrectangle【乘法分配律】【最大子矩阵】

    CF1060C Maximum Subrectangle 题意翻译 现在给出一个长度为N的a数列,一个长度为M的b数列. 现在需要构造出一个矩阵c,其中ci,j​=ai​×bj​.再给出一个x,请在矩 ...

  2. BZOJ 1022 [SHOI2008]小约翰的游戏John AntiNim游戏

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1475  Solved: 932[Submit][ ...

  3. POJ 2356 Find a multiple 抽屉原理

    从POJ 2356来体会抽屉原理的妙用= =! 题意: 给你一个n,然后给你n个数,让你输出一个数或者多个数,让这些数的和能够组成n: 先输出一个数,代表有多少个数的和,然后再输出这些数: 题解: 首 ...

  4. 监听当点击微信等app的返回按钮或者浏览器的上一页或后退按钮的事件

    在实际的应用中,我们常常需要实现在移动app和浏览器中点击返回.后退.上一页等按钮实现自己的关闭页面.调整到指定页面或执行一些其它操作的 需求,那在代码中怎样监听当点击微信.支付宝.百度糯米.百度钱包 ...

  5. C# 推送模板

    C#推送模板.安卓个推.消息推送 http://docs.getui.com/server/csharp/template/

  6. gridview DataFormatString 属性设置须知

    设置 DataFormatString 进行格式化数据时默认情况下是不会起作用的还有设置HtmlEncode = "false" 具体为什么?以下几点1. 在GridView中的a ...

  7. blkblock工具1

    http://www.ibm.com/developerworks/cn/linux/l-cn-perf1/ http://blog.chinaunix.net/uid-24774106-id-409 ...

  8. Adding an instance to a MEF container

    How can you add an already created instance to a MEF container/cataloge to use when resolving Import ...

  9. win8.1快速启动选项突然消失了怎么办?

    win8开始提供的快速启动功能是一种混合式的休眠模式,Windows系统 在关机时将系统的信息保存到硬盘上的一个文件中来实现下一次的快速启动.当再次启动电脑时, Windows 使用该系统信息文件来恢 ...

  10. HTML5 Canvas,WebGL,CSS Shaders,GLSL的暧昧关系

    一.前面的所以然 技术的发展日新月异,说不定回家钓几天鱼,就出来个新东西了.新事物新技术发展的初期,你无法预见其未来之趋势,生命诚可贵,没有必要花过多时间深入研究这些新东西,不过,知道了大概,了解个全 ...