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 行为类似于数组,但可以根据要求 ...
随机推荐
- 【Codeforces 949D】Shake It! 【动态规划】
参考: http://blog.csdn.net/gjghfd/article/details/77824901 所求的是满足条件的图中“不同构”的数量,意味着操作的顺序是可以忽略的.考虑若干次操作后 ...
- python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)
在上一篇blog:python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 中介绍了python中的tkinter的一些东西,你可能对tkinter有一定的了解了.这篇b ...
- windows下tomcat的安装配置
一.下载相应的JDK以及tomcat的版本 JDK:jdk-8u131-windows-x64 tomcat:apache-tomcat-8.5.23-windows-x64.zip 二.JDK的安装 ...
- ROS知识(14)----局部避障的动态窗口算法(DWA)及其调试的方法
Dynamic Window Approach(DWA)是重要的局部轨迹规划算法,ROS中使用了DWA算法获得了很好的局部路径规划的效果.具体的教程可参考官方的导航调试资料Navigation Tun ...
- oracle中的术语
数据库:数据库是实实在在存在的文件,一个数据库文件体系中大致包含(数据文件DBF.控制文件CTL.日志文件LOG) 数据库实例:每个数据库都会有一个数据库实例与之对应,外界环境要通过与数据库实例的交互 ...
- HDU 3487 Play with Chain (splay tree)
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HUST 1017 Exact cover(DLX精确覆盖)
Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- apple developer D-U-N-S® Number
From:https://developer.apple.com/support/D-U-N-S/ The D-U-N-S Number is a unique nine-digit number t ...
- 报错:不允许保存更改。您所做的更改要求删除并重新创建以下表……
在使用SQL Server 2008为某个表添加列的时候出现启用了"阻止保存要求重新创建表的更改问题的设置方法..."报错: 解决方法: 工具--选项--Designers-- ...
- 转 UIActivityIndicatorView、UIProgressView 活动与进度指示器-IOS开发
活动指示器(UIActivityIndicatorView)可以告知用户有一个操作正在进行中.进度指示器(UIProgressView )也具有同样功能,而且还可以告知用户离操作结束还多远. 这两个指 ...