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. CF1060C Maximum Subrectangle【乘法分配律】【最大子矩阵】

    CF1060C Maximum Subrectangle 题意翻译 现在给出一个长度为N的a数列,一个长度为M的b数列. 现在需要构造出一个矩阵c,其中ci,j​=ai​×bj​.再给出一个x,请在矩 ...

  2. bzoj 2150 最小路径覆盖

    最小路径覆盖问题是:给定一个DAG,该DAG的一个路径覆盖是一个路径的集合,使得每个点属于且仅属于其中一条路径,问题就是求一个大小最小的路径集合. 做法是将每个点A拆成两个点A1,A2,如果A-> ...

  3. Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. Git_自定义Git

    在安装Git一节中,我们已经配置了user.name和user.email,实际上,Git还有很多可配置项. 比如,让Git显示颜色,会让命令输出看起来更醒目: $ git config --glob ...

  5. C#情怀与未来,怨天尤人还是抓住机会,能否跟上dnc新时代浪潮?

    C#情怀与未来,怨天尤人还是抓住机会,能否跟上dnc新时代浪潮?   经常看到有.NET圈子在讨论是否应该转其它语言   C#情怀是一方面,如果觉得C#未来没前途,光靠情怀是撑不住的, 建议对C#未来 ...

  6. cloc 统计代码行数工具

    cloc 统计代码行数工具 官网地址:http://cloc.sourceforge.net/ 下载完成后 会生成一个.exe文件 需要修改文件名为 cloc.exe 然后把这个文件拷贝到需要统计的根 ...

  7. MsDepSvc 启动失败

    MsDepSvc 使用80端口,用于 Microsoft Web Deploy 3.6 的远程代理服务. 如果80端口被占用,则启动失败.我的是被phpstudy软件占用,所以启动失败.

  8. VisualSVN Server 修改用户密码

    VisualSVN Server是非常方便好用的SVN服务器端软件,但有个问题,你在服务器端创建了用户名密码后,用户无法自己修改密码,据说VisualSVN的客户端可以修改用户密码,但客户端是收费软件 ...

  9. nginx重启命令方法(linux,centos,ubuntu)总结

    原文:http://www.111cn.net/sys/nginx/62915.htm 平滑重启 如果服务器正在运行的Nginx要进行升级.添加或删除模块时,我们需 要停掉服务器并做相应修改,这样服务 ...

  10. Android跳转各种系统设置界面-总结

    来自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0921/8536.html View btn1 = this.findVi ...