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的更多相关文章

  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                             行为类似于数组,但可以根据要求 ...

随机推荐

  1. VC 调用 Python

    //file:py.h BOOL InitPython(); BOOL ClosePython(); ======================== //file:py.cpp #include & ...

  2. Struts2 JSONObject的使用

    一.jar包 使用之前必须引入所须要的jar包,这里包含Struts2和JSONObject各自所必须的 Struts2: commons-fileupload-1.2.1.jarcommons-io ...

  3. Linux下的两个经典宏定义 转

    http://www.linuxidc.com/Linux/2016-08/134481.htm http://www.linuxidc.com/Linux/2013-01/78003.htm htt ...

  4. 关于TFS2010 远程无法创建团队项目的若干问题总结

    今天遇到一个TFS的问题,折腾了好几个小时,故将其记录,给有遇到类似问题的朋友一些参考. 1.本文前提:服务器端只安装了TFS2010,本地没有安装Visual Studio 2010,因此不能在服务 ...

  5. Matlab绘图控制命令

    [转载] 图形的控制与表现 (Figure control and representation) MATLAB提供的用于图形控制的函数和命令: axis:  人工选择坐标轴尺寸.     clf: ...

  6. MyEclipse Web项目调试

    1.发布项目 2.启动服务 服务有两种启动方式,Run Server和Debug Server Run Server是运行模式,Debug Server是调试模式 使用Debug Server模式启动 ...

  7. EF三种加载方法

    EF性能之关联加载   鱼和熊掌不能兼得 ——中国谚语 一.介绍 Entity Framework作为一个优秀的ORM框架,它使得操作数据库就像操作内存中的数据一样,但是这种抽象是有性能代价的,故鱼和 ...

  8. 自用广告过滤规则,整合xwhyc大大的,非常小才79K

    xwhyc大大 好久没更新了,自己弄了一点 更新: $third-party选项过滤多个站点的第三方广告:dy1000.com.yatu.tv,greasyfork.org 主流视频站点,请配合我的脚 ...

  9. Kafka开发环境搭建(五)

    如果你要利用代码来跑kafka的应用,那你最好先把官网给出的example先在单机环境和分布式环境下跑通,然后再逐步将原有的consumer.producer和broker替换成自己写的代码.所以在阅 ...

  10. With Visual Studio, Open Same File In Two Windows, Updates Reflected in Both

    I’ve always been frustrated in Visual Studio (all versions I can remember including latest vs2012) w ...