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里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...
随机推荐
- Codeforces Round #355 (Div. 2) C. Vanya and Label 水题
C. Vanya and Label 题目连接: http://www.codeforces.com/contest/677/problem/C Description While walking d ...
- Git_自定义Git
在安装Git一节中,我们已经配置了user.name和user.email,实际上,Git还有很多可配置项. 比如,让Git显示颜色,会让命令输出看起来更醒目: $ git config --glob ...
- 字符串转码【String.getBytes()和new String()】
在Java中,String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示,如 byte[] b_gbk = "中&q ...
- HTML5学习笔记1
1.HTML5概述 继html4和xhtml1.0后的超文本标记语言最新版本.最重要的三项技术:html5核心规范(标签元素),CSS3,JavaScript2008年发布,主要为了补全功能.特点:1 ...
- MySQL运维开发相关的所有工具
http://www.ruzuojun.com/topic/592.html Percona Toolkit 安装使用 http://cenalulu.github.io/mysql/mysql- ...
- C#编程(五)----流程控制
流控制(控制语句) 程序的代码不是按照从上往下执行的,是按照控制语句执行的. 条件语句 C#中有两个控制语句:if语句还有switch语句 1.if语句 C#中继承了C和C++中的if语句,语法直 ...
- Extjs GridPanel 监听事件 行选中背景
Extjs设置GridPanel选中行背景色和选中单元格背景色 var view = grid.getView(); view.getRow(index).style.backgroundColor ...
- Appium+python自动化3-启动淘宝app
前言 前面两篇环境已经搭建好了,接下来就是需要启动APP,如何启动app呢?首先要获取包名,然后获取launcherActivity.获取这两个关键东西的方法很多,这里就不一一多说,小伙伴们可以各显神 ...
- 微商营销实战技巧分享,轻松月入10W
如今能够说是移动互联时代.在这个时代,微信眼下能够说是当之无愧的移动应用,依据报道,眼下微信有7个多亿的用户,怪不得那么多人看到微商的时代,一大批人開始涌入微商,导致如今微信上卖产品都已经泛滥了,导致 ...
- matlab 投影
function[l]= Gray(I) % I: The name of image A=imread(I);m=0;n=0;[m,n]= size(A);Hproj=zeros(m,1);Vpro ...