【C++ Primer | 10】泛型算法
#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,下面的图片解释的很清楚。

【C++ Primer | 10】泛型算法的更多相关文章
- c++ primer 11 泛型算法
使用泛型算法必须包含头文件#inlucde <algorithm> 标准库还定义一组泛化的算术算法,其命名习惯与泛型算法相同,包含头文件#include <numeric> f ...
- C++ Primer 5th 第10章 泛型算法
练习10.1:头文件algorithm中定义了一个名为count的函数,它类似find,接受一对迭代器和一个值作为参数.count返回给定值在序列中出现的次数.编写程序,读取int序列存入vector ...
- [C++ Primer] : 第10章: 泛型算法
概述 泛型算法: 称它们为"算法", 是因为它们实现了一些经典算法的公共接口, 如搜索和排序; 称它们是"泛型的", 是因为它们可以用于不同类型的元素和多种容器 ...
- C++ Primer : 第十章 : 泛型算法 之 只读、写和排序算法
大多数算法都定义在<algorithm>头文件里,而标准库还在头文件<numeric>里定义了一组数值泛型算法,比如accumulate. ● find算法,算法接受一对迭代 ...
- 【足迹C++primer】30、概要(泛型算法)
概要(泛型算法) 大多数算法的头文件中定义algorithm在. 标准库也是第一个文件numeric它定义了一套通用算法. #include<iostream> #include<n ...
- C++ Primer笔记6_STL之泛型算法
1.泛型算法: 大多数算法定义在头文件algorithm中.标准库还在头文件numeric中定义了一组数值泛型算法 仅仅读算法: 举例: find函数用于找出容器中一个特定的值,有三个參数 int v ...
- 【c++ Prime 学习笔记】第10章 泛型算法
标准库未给容器添加大量功能,而是提供一组独立于容器的泛型算法 算法:它们实现了一些经典算法的公共接口 泛型:它们可用于不同类型的容器和不同类型的元素 利用这些算法可实现容器基本操作很难做到的事,例如查 ...
- C++ Primer 读书笔记:第11章 泛型算法
第11章 泛型算法 1.概述 泛型算法依赖于迭代器,而不是依赖容器,需要指定作用的区间,即[开始,结束),表示的区间,如上所示 此外还需要元素是可比的,如果元素本身是不可比的,那么可以自己定义比较函数 ...
- C++ Primer 学习笔记_45_STL实践与分析(19)--泛型算法的结构
STL实践与分析 --泛型算法的结构 引言: 正如全部的容器都建立在一致的设计模式上一样,算法也具有共同的设计基础. 算法最主要的性质是须要使用的迭代器种类.全部算法都指定了它的每一个迭代器形參可使用 ...
- C++ 泛型算法
<C++ Primer 4th>读书笔记 标准容器(the standard container)定义了很少的操作.标准库并没有为每种容器类型都定义实现这些操作的成员函数,而是定义了一组泛 ...
随机推荐
- Faster_RCNN 1.准备工作
总结自论文:Faster_RCNN,与Pytorch代码: 代码结构: simple-faster-rcnn-pytorch.py data __init__.py dataset.py util. ...
- C++设计模式之工厂模式(1)
关于设计模式的作用: “帮助我们将应用组织成容易了解,容易维护,具有弹性的架构,建立可维护的OO系统,要诀在于随时想到系统以后可能需要的变化以及应付变化的原则.” 具体可参考:https://www. ...
- 修改主机IP地址
IP:由192.168.1.1~192.168.1.254. 先使用电脑以动态IP的方式自动获取ip地址联网,然后通过以下方法查询子网掩码和默认网关地址:
- lvs为何不能完全替代DNS轮询
1)接入层架构要考虑的问题域为:高可用.扩展性.反向代理+扩展均衡 2)nginx.keepalived.lvs.f5可以很好的解决高可用.扩展性.反向代理+扩展均衡的问题 3)水平扩展scale o ...
- #ifndef#define#endif的用法(整理)
[转] #ifndef#define#endif的用法(整理) 原作者:icwk 文件中的#ifndef 头件的中的#ifndef,这是一个很关键的东西.比如你有两个C文件,这两个C文件都in ...
- 带jdk15类似的jar配置
针对部分jar文件名带有jdk15结尾的依赖配置时,需要添加<classifier>标签进行区分 比如针对:json-lib-2.4-jdk15.jar的jar依赖配置 <depen ...
- druid:java代码创建连接池
PropertiesDB 是一个读取配置文件的类,也可以不用,每个参数直接用String代替. public DataSource dataSource(PropertiesDB properties ...
- python学习第41天
# 索引 # 认识mysql中的key # index key 普通索引,能够加速查询,辅助索引 # unique key 唯一 + 索引,辅助索引 # primary key 唯一 + 非空 + 聚 ...
- Git操作----删除untracked files
# 删除 untracked files git clean -f # 连 untracked 的目录也一起删掉 git clean -fd # 连 gitignore 的untrack 文件/目录也 ...
- ASP.NET的路由系统:路由映射
总的来说,我们可以通过RouteTable的静态属性Routes得到一个基于应用的全局路由表,通过上面的介绍我们知道这是一个类型的RouteCollection的集合对象,我们可以通过调用它的MapP ...