#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<fstream>
using namespace std; void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto unique_end = unique(words.begin(), words.end());
words.erase(unique_end, words.end());
} void display(vector<string> &words)
{
for (auto c : words)
cout << c << " ";
cout << endl;
} int main()
{
ifstream in("test.txt");
if (!in)
{
cout << "打开文件失败" << endl;
exit();
} vector<string> words;
string str;
while (in >> str)
words.push_back(str);
elimDups(words);
display(words);
return ;
}

输出结果:

 定制操作

示例代码:

 #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<fstream>
using namespace std; void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto unique_end = unique(words.begin(), words.end());
words.erase(unique_end, words.end());
} void biggies(vector<string> &words, vector<string>::size_type sz)
{
elimDups(words); //将单词按字典排序,删除重复单词
stable_sort(words.begin(), words.end(), [](const string &a, const string &b) { return a.size() < b.size(); });
auto wc = find_if(words.begin(), words.end(), [sz](const string &a) { return a.size() >= sz; });
auto count = words.end() - wc;
for_each(wc, words.end(), [](const string &s) { cout << s << " "; });
cout << endl;
} int main()
{
ifstream in("test.txt");
if (!in)
{
cout << "打开文件失败" << endl;
exit();
} vector<string> words;
string str;
while (in >> str)
words.push_back(str);
auto sz = ;
biggies(words, sz);
return ;
}

输出结果:

再探迭代器

3. 反向迭代器

 #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 ;
}

输出结果:

 #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,下面的图片解释的很清楚。

最新文章

  1. Django Form and Modelform Admin定义 高级查询)
  2. 错误解决:SharePoint Designer 2010编辑后,出现数据源控件未能执行插入命令,data source control failed to execute the insert command
  3. jsp_数据库的连接
  4. STL之stack栈
  5. Hadoop上路-01_Hadoop2.3.0的分布式集群搭建
  6. 一个IO的传奇一生 系列 存储之道
  7. 并行计算vs分布式计算
  8. php 中PHP_EOL使用
  9. Linux 常用命令汇总
  10. 采用Spring AOP+Log4j记录项目日志
  11. Java程序员的Golang入门指南(下)
  12. .NET CORE 中使用AutoMapper进行对象映射
  13. Asp.Net Core 轻松学-被低估的过滤器
  14. NIO小纪
  15. 【Social listening实操】从社交媒体传播和文本挖掘角度解读《欢乐颂2》
  16. 实操演练!MathType几个绝妙小技巧!
  17. Swift 开发语法
  18. Python print format() 格式化内置函数
  19. 小程序常用API介绍
  20. spark在yarn-cluster上面执行报错

热门文章

  1. k64 datasheet学习笔记31---External Bus Interface (FlexBus)
  2. 利用微信企业号的告警功能,联动检测ICMP的shell脚本
  3. git与eclipse集成之文件回退
  4. office xml 方式
  5. FFmpeg Commits on May 30, 2017 remove libschroedinger &amp; libnut
  6. thinkphp中的内置操作数据库与mysql中的函数汇总
  7. python-序列化模块
  8. Codeforces 487E Tourists [广义圆方树,树链剖分,线段树]
  9. Confluence 6 管理协同编辑
  10. Confluence 6 编辑自定义 Decorators