STL学习笔记(迭代器配接器)
Reverse(逆向)迭代器
Reverse迭代器是一种配接器。 重新定义递增运算和递减运算。使其行为正好倒置。
如果你使用这类迭代器,算法将以逆向次序处理元素。所有标准容器都允许使用Reverse迭代器来遍历元素。下面是个例子:
#include <iostream>
#include <list>
#include <algorithm>
using namespace std; void print(int elem)
{
cout<<elem<<' ';
} int main()
{
list<int> coll;
for(int i=;i<=;++i)
coll.push_back(i);
for_each(coll.begin(),coll.end(),print);
cout<<endl;
for_each(coll.rbegin(),coll.rend(),print);
cout<<endl;
}
Insert(安插型)迭代器
通过这种迭代器,算法可以执行安插行为而非覆盖行为。它提供以下操作

C++标准程序库提供三种Insert迭代器:back Inserters、front inserters、general Inserters。它们之间的区别在于插入位置。
事实上它们各自调用所属容器中不同的成员函数。

显然,容器本身必须支持Insert迭代器所调用的函数,否则该种Insert迭代器就不可用。
下面展示了back inserters的用法
#include <iostream>
#include <vector>
#include <algorithm>
#include "print.cpp"
using namespace std; int main()
{
vector<int> coll;
back_insert_iterator<vector<int> > iter(coll);
*iter=;
iter++;
*iter=;
iter++;
*iter=;
PRINT_ELEMENTS(coll);
back_inserter(coll)=;
back_inserter(coll)=;
PRINT_ELEMENTS(coll);
coll.reserve(*coll.size());
copy(coll.begin(),coll.end(),back_inserter(coll));
PRINT_ELEMENTS(coll);
}
Stream(流)迭代器
我们可以通过Stream迭代器把stream当成算法的原点和起点。
一个istream迭代器可用来从input stream中读取元素,而一个ostream迭代器可以用来对output stream写入元素。
1.Ostream迭代器
ostream迭代器可以将被赋予的值写入output stream中。如此一来算法就可以使用一般的迭代器接口直接对stream执行涂写动作。下面列出ostream迭代器的各个操作函数。

下面演示ostream迭代器的用法
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std; int main()
{
ostream_iterator<int> intWriter(cout,"\n");
*intWriter=;
intWriter++;
*intWriter=;
intWriter++;
*intWriter=-;
vector<int> coll;
for(int i=;i<=;++i)
coll.push_back(i);
copy(coll.begin(),coll.end(),ostream_iterator<int>(cout));
cout<<endl;
copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," < "));
cout<<endl;
}
2.istream迭代器
istream迭代器用来从input stream读取元素。透过istream迭代器,算法可以从stream中直接读取数据。
下面是istream迭代器的各项操作函数

下面展示istream迭代器的各项操作
#include <iostream>
#include <iterator>
using namespace std; int main()
{
istream_iterator<int> intReader(cin);
istream_iterator<int> intReaderEOF;
while(intReader!=intReaderEOF)
{
cout<<"once: "<<*intReader<<endl;
cout<<"once again: "<<*intReader<<endl;
++intReader;
}
}
STL学习笔记(迭代器配接器)的更多相关文章
- STL学习笔记:空间配置器allocator
allocator必要接口: allocator::value_type allocator::pointer allocator::const_pointer allocator::referenc ...
- 《Head first设计模式》学习笔记 – 迭代器模式
<Head first设计模式>学习笔记 – 迭代器模式 代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 爆炸性新闻:对象村餐厅和对象村煎饼屋合并了!真是个 ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor
Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...
- 【STL学习笔记】一、STL体系
目录 1.标准库以header files形式呈现 2.namespce命名空间 3.STL与OO 4.STL六组件及其关系 5.STL组件例子 6.range-based for statement ...
- ArcGIS案例学习笔记2_2_模型构建器和山顶点提取批处理
ArcGIS案例学习笔记2_2_模型构建器和山顶点提取批处理 计划时间:第二天下午 背景:数据量大,工程大 目的:自动化,批处理,定制业务流程,不写程序 教程:Pdf/343 数据:chap8/ex5 ...
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
随机推荐
- 经典 Javascript 正则表达式
正则表达式用于字符串处理,表单验证等场合,实用高效,但用到时总是不太把握,以致往往要上网查一番.我将一些常用的表达式收藏在这里,作备忘之用.匹配中文字符的正则表达式: }
- luogu 2709 小B的询问 莫队
题目链接 Description 小B有一个序列,包含\(N\)个\(1-K\)之间的整数.他一共有\(M\)个询问,每个询问给定一个区间\([L..R]\),求\(\sum_{i=1}^{K}c_i ...
- ldd命令【转】
转自:http://www.cnblogs.com/wanghetao/p/3779611.html ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库. 参数说明: --versi ...
- 【原创】Linux环境下的图形系统和AMD R600显卡编程(3)——AMD显卡简介
早期的显卡仅用于显示,后来显卡中加入了2D加速部件,这些部件用于做拷屏,画点,画线等操作.随着游戏.三维模拟以及科学计算可视化等需要,对3D的需求逐渐增加,早期图形绘制工作由CPU来完成,要达到真实感 ...
- Leap Motion颠覆操控体验的超精致手势追踪技术【转】
转自:http://www.cnblogs.com/emouse/archive/2013/02/28/2936689.html 先来看两段简介视频: 看了介绍视频后,对如此次超高精度的手势追踪非常好 ...
- Selenium2+python自动化9-CSS定位语法【转载】
前言 大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css ...
- 第五步:Lucene创建索引
package cn.lucene; import java.io.IOException; import java.nio.file.Paths; import java.util.Date; im ...
- AC日记——[国家集训队2011]旅游(宋方睿) cogs 1867
[国家集训队2011]旅游(宋方睿) 思路: 树链剖分,边权转点权: 线段树维护三个东西,sum,max,min: 当一个区间变成相反数时,sum=-sum,max=-min,min=-max: 来, ...
- 腾讯云通信WebIM事件回调的坑~
最近在开过工作中用到了腾讯IM的功能,由于业务的需要主要使用到了: 1.loginInfo 用户登录,用户信息 2.getRecentContactList 获得最近联系人 3.getLastGrou ...
- Python的网络编程[0] -> socket[2] -> 利用 socket 建立 TCP/UDP 通信
Socket 目录 socket 的 TCP/IP 通信基本建立过程 socket 的 UDP 通信基本建立过程 socket 的 UDP 广播式通信基本建立过程 socket 的多线程通信建立过程 ...