【C++ Primer | 10】再探迭代器
插入迭代器
1. 测试代码:
#include<iostream>
#include<vector>
#include<list>
#include<iterator>
#include<algorithm>
using namespace std; void display(list<int> li)
{
for (auto c : li)
cout << c << " ";
cout << endl;
} int main()
{
vector<int> vi = { , , , , , , , , , };
list<int> li1, li2, li3; unique_copy(vi.begin(), vi.end(), inserter(li1, li1.begin()));
display(li1); unique_copy(vi.begin(), vi.end(), back_inserter(li2));
display(li2); unique_copy(vi.begin(), vi.end(), front_inserter(li3));
display(li3);
system("pause");
return ;
}
输出结果:
iostream迭代器
1. 测试代码:
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std; int main()
{
istream_iterator<int> in_iter(cin);
istream_iterator<int> eof;
vector<int> vi;
while (in_iter != eof)
vi.push_back(*in_iter++); sort(vi.begin(), vi.end()); ostream_iterator<int> out_iter(cout, " ");
copy(vi.begin(), vi.end(), out_iter);
return ;
}
输出结果:
2. 测试代码:
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std; int main()
{
istream_iterator<int> in_iter(cin);
istream_iterator<int> eof;
vector<int> vi;
while (in_iter != eof)
vi.push_back(*in_iter++); sort(vi.begin(), vi.end()); ostream_iterator<int> out_iter(cout, " ");
unique_copy(vi.begin(), vi.end(), out_iter);
return ;
}
输出结果:
反向迭代器
1. 测试代码:
#include<iostream>
#include<vector>
#include<iterator>
using namespace std; int main()
{
vector<int> vec = { , , , , , , , , , };
for (auto r_iter = vec.crbegin(); r_iter != vec.crend(); ++r_iter)
cout << *r_iter << " ";
cout << endl;
return ;
}
输出结果:
2. 测试代码:
#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>
using namespace std; void print(int elem)
{
cout << elem << ' ';
} int main()
{
deque<int> coll;
for (int i = ; i <= ; ++i)
coll.push_back(i); deque<int>::iterator pos1;
pos1 = find(coll.begin(), coll.end(), ); deque<int>::iterator pos2;
pos2 = find(coll.begin(), coll.end(), );
for_each(pos1, pos2, print);
cout << endl; deque<int>::reverse_iterator rpos1(pos1);
deque<int>::reverse_iterator rpos2(pos2);
for_each(rpos2, rpos1, print);
cout << endl;
return ;
}
输出结果:
【分析】
代码首先在一个deque中插入1到9,然后查找元素值为2和7的位置,分别赋值给迭代器pos1和pos2,然后输出,由于STL中的操作总是左开右闭的区间,即[2,7),所以输出2 3 4 5 6,7不会输出。
接下来将迭代器转换成逆向迭代器,再次输出,对于反向迭代器,由于是反向,所以按逻辑来说它是左开右闭的(这里我尝试了rpos2为iterator.end(),rpos1为iterator.begin(),此时输出全部),即(7,2](事实上还是左闭右开,只不过此时的左和iterator顺序一样)。所以输出6 5 4 3 2,下面的图片解释的很清楚。
【C++ Primer | 10】再探迭代器的更多相关文章
- 【C++ Primer 第10章】再探迭代器
反向迭代器 • 反向迭代器就是在容器中从尾元素向首元素反向移动的迭代器.对于反向迭代器,递增(以及递减)操作的含义会颠倒过来. • 递增一个反向迭代器(++it)会移动到前一个元素:递减一迭代器(-- ...
- C++ Primer :第十章 :泛型算法之再探迭代器以及其他算法
除了为每个容器定义的迭代器之外,标准库在头文件<iterator>还定义了额外集中迭代器, 包括: 插入迭代器,这些迭代器被绑定到一个容器上,可以向容器插入元素. 流迭代器, 这些迭 ...
- 10.4 再探迭代器-插入/IO/反向
10.4.1 插入迭代器 插入迭代器接受一个容器,生成一个迭代器,通过向该迭代器赋值可以实现向容器添加元素 (1)back_inserter: 接受一个参数, 示例: auto iter = back ...
- 【C++ Primer | 10】泛型算法
#include<iostream> #include<algorithm> #include<vector> #include<string> #in ...
- C++ Primer 学习笔记_43_STL实践与分析(17)--再谈迭代器【中】
STL实践与分析 --再谈迭代器[中] 二.iostream迭代[续] 3.ostream_iterator对象和ostream_iterator对象的使用 能够使用ostream_iterator对 ...
- C++ Primer 学习笔记_44_STL实践与分析(18)--再谈迭代器【下】
STL实践与分析 --再谈迭代器[下] 三.反向迭代器[续:习题] //P355 习题11.19 int main() { vector<int> iVec; for (vector< ...
- 【再探backbone 02】集合-Collection
前言 昨天我们一起学习了backbone的model,我个人对backbone的熟悉程度提高了,但是也发现一个严重的问题!!! 我平时压根没有用到model这块的东西,事实上我只用到了view,所以昨 ...
- 再探jQuery
再探jQuery 前言:在使用jQuery的时候发现一些知识点记得并不牢固,因此希望通过总结知识点加深对jQuery的应用,也希望和各位博友共同分享. jQuery是一个JavaScript库,它极大 ...
- 再探java基础——break和continue的用法
再探java基础——break和continue的用法 break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后 ...
随机推荐
- Python3学习笔记04-运算符
算术运算符 + 加两个对象相加 - 减得到负数或是一个数减去另一个数 * 乘两个数相乘或是返回一个被重复若干次的字符串 / 除x 除以 y % 取模返回除法的余 ...
- python操作三大主流数据库(5)python操作mysql⑤使用Jinja2模板提取优化页面展示
python操作mysql⑤使用Jinja2模板提取优化页面展示 在templates目录下的index.html.cat.html等页面有一些共同的元素,代码比较冗余可以使用模板提取公共代码,在各网 ...
- 程序包管理dpkg、apt-get、服务端openssh-server与客户端Xshell设置及lrzsz安装使用
一.程序包管理器 dpkg.apt-get 1.dpkg 安装:sudo dpkg -i cmatrix_1.2a-5build3_amd64.deb 卸载:sudo dpkg -r cmatrix ...
- layui 常见的表单元素
第一步:引用文件 效果图(日期.文件上传在下面): <form class="layui-form" action=""> <div clas ...
- Linux学习之CentOS(二)--初识linux的一些常用命令
Linux学习之CentOS(二)--初识linux的一些常用命令 在VM上安装完了CentOS6.4以后,看着linux系统成功跑起来,心里小激动了一把......但是前方学习的道路还很遥远... ...
- sql 的是四个排名函数
四个排名函数的用法: http://www.cnblogs.com/xhyang110/archive/2009/10/27/1590448.html 字符串分割:http://www.cnblogs ...
- mysql运维
反反复复装了好多次的mysql,上学的时候从来没有考虑过稳定性,装起来,能跑通,增删改查没有问题万事大吉.参与工作后参与平台搭建和维护,平台的稳定性是首先必须要考虑的问题,之前装mysql使用经历了密 ...
- Modbus库开发笔记之十一:关于Modbus协议栈开发的说明
对于Modbus协议栈的整个开发内容,前面已经说得很清楚了,接下来我们说明一下与开发没有直接关系的内容. 首先,关于我为什么开发这个协议栈的问题.我们的初衷只是想能够在开发产品时不用每次都重写这一部分 ...
- 机器学习之线性回归---logistic回归---softmax回归
在本节中,我们介绍Softmax回归模型,该模型是logistic回归模型在多分类问题上的推广,在多分类问题中,类标签 可以取两个以上的值. Softmax回归模型对于诸如MNIST手写数字分类等问题 ...
- 在java中,OOA是什么?OOD是什么?OOP是什么?
注:本文来源于< 在java中,OOA是什么?OOD是什么?OOP是什么?> 在java中,OOA是什么?OOD是什么?OOP是什么? OOA Object-Oriented Anal ...