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 行为类似于数组,但可以根据要求 ...
随机推荐
- CentOS系统下中文文件名乱码
原文来自:http://www.zhukun.net/archives/7434 CentOS系统下中文文件名乱码 2014/09/01Linux运维centos.Linuxbear 从windows ...
- 【POJ】1486:Sorting Slides【二分图关键边判定】
Sorting Slides Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5390 Accepted: 2095 De ...
- play framework系列之打包war
概览 Play framwork 是我们一直在使用的框架,从刚开始的简单使用,乱起八糟的jar包引用,项目组成员之间的下载项目之后的引用问题等,遇到各种问题,我都一一解决,我将在这个系列中奉上解决方案 ...
- INFORMATION_SCHEMA.COLUMNS-表的字段信息
当前数据库中当前用户可以访问的每一个列在该视图中占一行.INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects.spt_data type_info.systypes.sy ...
- lykchat+zabbix实现微信告警
http://www.ttlsa.com/zabbix/lykchat-zabbix-wechat-alert/
- iOS开发者帐号申请指南
iOS开发者的申请流程如果你是一个开发团队,在你打算掏腰包购买iOS开发者授权之前,最好先问一下你的同事,是否已经有人获得了开发许可,因为一个开发许可一年内最多可以授权给111个设备来开发测试.如果你 ...
- velocity的一些优化记录
背景 前段时间做了个项目,主要优化一个产品页面.整个优化过程中,针对velocity的分析过程占了比较大的比重,这里做一下整理和记录. 描述 velocity版本: <dependency> ...
- [翻译] FlatUIKit
FlatUIKit FlatUIKit is a collection of iOS components styled with the "Flat UI" aesthetic ...
- Mongodb后台daemon方式启动
Mongodb可以通过命令行方式和配置文件的方式来启动,具体命令如下: 命令行: [root@localhost mongodb]# ./bin/mongod --dbpath=/data/db 配置 ...
- eclipse 设置打开java文件代码自动折叠
eclipse 设置打开java文件代码自动折叠 java: windows/preference/java/editor/folding->enable folding 可以在里面设置所要折叠 ...