STL - 容器 - List
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的更多相关文章
- STL容器
啦啦啦,今天听啦高年级学长讲的STL容器啦,发现有好多东西还是有必要记载的,毕竟学长是身经百战的,他在参加各种比赛的时候积累的经验可不是一天两天就能学来的,那个可是炒鸡有价值的啊,啊啊啊啊啊 #inc ...
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- STL容器删除元素的陷阱
今天看Scott Meyers大师的stl的用法,看到了我前段时间犯的一个错误,发现我写的代码和他提到错误代码几乎一模一样,有关stl容器删除元素的问题,错误的代码如下:std::vector< ...
- 【转】c++中Vector等STL容器的自定义排序
如果要自己定义STL容器的元素类最好满足STL容器对元素的要求 必须要求: 1.Copy构造函数 2.赋值=操作符 3.能够销毁对象的析构函数 另外: 1. ...
- GDB打印STL容器内容
GDB调试不能打印stl容器内容,下载此文件,将之保存为~/.gdbinit就可以使用打印命令了. 打印list用plist命令,打印vector用pvector,依此类推. (gdb) pvecto ...
- STL容器迭代器失效分析
连续内存序列容器(vector, string, deque) 对于连续内存序列STL容器,例如vector,string,deque,删除当前iterator会使得后面所有的iterator都失效, ...
- STL容器的适用情况
转自http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/ ly; mso-default-props:yes; m ...
- STL容器的遍历删除
STL容器的遍历删除 今天在对截包程序的HashTable中加入计时机制时,碰到这个问题.对hash_map中的每个项加入时间后,用查询函数遍历hash_map,以删除掉那些在表存留时间比某个阈值长的 ...
- STL容器与配接器
STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector 行为类似于数组,但可以根据要求 ...
- STL容器的本质
http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...
随机推荐
- 【BZOJ】2760: [JLOI2011]小A的烦恼【字符串模拟】
2760: [JLOI2011]小A的烦恼 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 406 Solved: 258[Submit][Statu ...
- django: ListView解读
[转载注明出处: http://www.cnblogs.com/yukityan/p/8039041.html ] django内置列表视图: # 导入 from django.views.gener ...
- MyTalkStuffHomeIcon-2
圆形.高清头像素材专用-2
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #279 (Div. 2) A. Team Olympiad 水题
#include<stdio.h> #include<iostream> #include<memory.h> #include<math.h> usi ...
- Git_撤销修改
自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txt Git is a distributed version co ...
- 【Go入门教程7】面向对象(method、指针作为receiver、method继承、method重写)
前面两章我们介绍了函数和struct,那你是否想过函数当作struct的字段一样来处理呢?今天我们就讲解一下函数的另一种形态,带有接收者(receiver)的函数,我们称为method method ...
- MVC无限级分类02,增删改查
继上一篇"MVC无限级分类01,分层架构,引入缓存,完成领域模型与视图模型的映射",本篇开始MVC无限级分类的增删改查部分,源码在github. 显示和查询 使用datagrid显 ...
- Java 内存释放
问题一什么叫垃圾回收机制 垃圾回收是一种动态存储管理技术它自动地释放不再被程序引用的对象按照特定的垃圾收集算法来实现资源自动回收的功能.当一个对象不再被引用的时候内存回收它占领的空间 ...
- [runtime] MAObjCRuntime
MAObjCRuntime 源码地址:(引入头文件MARTNSObject.h即可,非arc环境下) http://pan.baidu.com/s/1eQ6776U https://github.co ...