STL - 容器 - Forward List
forward list是一个行为受限的list, 不能走回头路。
它只提供前向迭代器, 而不提供双向迭代器。
eg:
rbegin(), rend(), crbegin(), crend()这些都不提供。
它不提供size()成员函数。
没有指向最末元素的anchor, 因此不提供back(), push_back(), pop_back()。
ForwardListTest.cpp:
#include <forward_list>
#include "../../Core/print.hpp"
#include "ForwardListTest.h" using namespace std; void ForwardListTest::findDemo()
{
forward_list<int> list = { , , , , , , , }; // find the position before the first even element
auto posBefore = list.before_begin();
for (auto pos = list.begin(); pos != list.end(); ++pos, ++posBefore)
{
if (*pos % == )
{
break; // element found
}
} // and insert a new element in front of the first even element
list.insert_after(posBefore, );
PRINT_ELEMENTS(list);
} void ForwardListTest::run()
{
printStart("findDemo()");
findDemo();
printEnd("findDemo()");
}
运行结果:
---------------- findDemo(): Run Start ----------------
1 42 2 3 4 5 97 98 99
---------------- findDemo(): Run End ----------------
自定义find_before_if操作:
#include "../../Core/findbefore.hpp" void ForwardListTest::findBeforeDemo()
{
forward_list<int> list = { , , , , , , , }; // find the position before the first even element
auto posBefore = find_before_if(list.before_begin(), list.end(),
[](int i) {
return i % == ;
}); // and insert a new element in front of the first even element
list.insert_after(posBefore, );
PRINT_ELEMENTS(list);
} void ForwardListTest::run()
{
printStart("findBeforeDemo()");
findBeforeDemo();
printEnd("findBeforeDemo()");
}
运行结果:
---------------- findBeforeDemo(): Run Start ----------------
1 42 2 3 4 5 97 98 99
---------------- findBeforeDemo(): Run End ----------------
完整Demo
void ForwardListTest::fullDemo()
{
// create two forward lists
forward_list<int> list1 = { , , , };
forward_list<int> list2 = { , , };
printLists("initial:", list1, list2); // insert six new element at the beginning of list2
list2.insert_after(list2.before_begin(), );
list2.push_front();
list2.insert_after(list2.before_begin(), { , , , });
printLists("6 new elems:", list1, list2); // insert all elements of list2 at the beginning of list1
list1.insert_after(list1.before_begin(),
list2.begin(), list2.end());
printLists("list2 into list1:", list1, list2); // delete second element and elements after element with value 99
list2.erase_after(list2.begin());
list2.erase_after(find(list2.begin(), list2.end(),
),
list2.end());
printLists("delete 2nd and after 99:", list1, list2); // sort list1, assign it to list2, and remove duplicates
list1.sort();
list2 = list1;
list2.unique();
printLists("sorted and unique:", list1, list2); // merge both sorted lists into list1
list1.merge(list2);
printLists("merged:", list1, list2);
}
运行结果:
---------------- fullDemo(): Run Start ----------------
initial:
list1: 1 2 3 4
list2: 77 88 99
6 new elems:
list1: 1 2 3 4
list2: 10 11 12 13 10 99 77 88 99
list2 into list1:
list1: 10 11 12 13 10 99 77 88 99 1 2 3 4
list2: 10 11 12 13 10 99 77 88 99
delete 2nd and after 99:
list1: 10 11 12 13 10 99 77 88 99 1 2 3 4
list2: 10 12 13 10 99
sorted and unique:
list1: 1 2 3 4 10 10 11 12 13 77 88 99 99
list2: 1 2 3 4 10 11 12 13 77 88 99
merged:
list1: 1 1 2 2 3 3 4 4 10 10 10 11 11 12 12 13 13 77 77 88 88 99 99 99
list2:
---------------- fullDemo(): Run End ----------------
STL - 容器 - Forward 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 行为类似于数组,但可以根据要求 ...
随机推荐
- Python168的学习笔记3
list.extend(),可以拓展list,a=(0,1),b=(2,3) a.extend(b),a就变成(0,1,2,3) 分割字符串(除去字符串中的,\/;之类的),如果用str.split( ...
- python开发_tkinter_图形随鼠标移动
做这个东西的时候,灵感源自于一个js效果: 两个眼睛随鼠标移动而移动 运行效果: =============================================== 代码部分: ===== ...
- Android WebView H5开发拾遗
上篇介绍了一些WebView的设置,本篇为一些补充项. 首先加载HTML5的页面时,会出现页面空白的现象,原因是需要开启 DOM storage API 功能: webSettings.setDomS ...
- 总结下git中一些常用命令
一.目录操作 1.cd 即change directory,改变目录,如 cd d:/www,切换到d盘的www目录. 2.cd .. cd+空格+两个点,回退到上一目录. 3.pwd 即 print ...
- sublime插件汇总
JsFormat javascript格式化 有时从网上扒了人家的js代码来学习学习,打开发现被压缩了,这时就能够用JsFormat插件格式化js代码,恢复未压缩时候的排版,挺给力的.按快捷键Ctrl ...
- SyncThingWin -- Run syncthing as a windows service
SyncThingWin Auto restart and minor bug fixes bloones released this on 23 Dec 2014 There is now an a ...
- XPROG-m编程器
XPROG-m编程器是为取代较早版本的XPROG编程器而设计的. XPROG-m编程器硬件完全与XPROG编程器向上兼容,还具有其它许多功能. 该XPROG - M支持摩托罗拉68HC05,68HC0 ...
- HTTP 协议中 Vary 的一些研究
经常抓包看 HTTP 请求的同学应该对 Vary 这个响应头字段并不陌生,它有什么用?用 PageSpeed 工具检查页面时,经常看到「Specify a Vary: Accept-Encoding ...
- redhat/centos使用service控制启动与关闭
原文地址: http://guodong810.blog.51cto.com/4046313/1285353 有时,我们自己安装了某个软件时,想让对这个服务更加容易的控制,在redhat/centos ...
- 转 SQL语句的添加、删除、修改多种方法
SQL语句的添加.删除.修改虽然有如下很多种方法,但在使用过程中还是不够用,不知是否有高手把更多灵活的使用方法贡献出来? 添加.删除.修改使用db.Execute(Sql)命令执行操作 ╔------ ...