【C++ Primer | 10】泛型算法
2024-10-12 13:30:33
#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,下面的图片解释的很清楚。
最新文章
- Django Form and Modelform Admin定义 高级查询)
- 错误解决:SharePoint Designer 2010编辑后,出现数据源控件未能执行插入命令,data source control failed to execute the insert command
- jsp_数据库的连接
- STL之stack栈
- Hadoop上路-01_Hadoop2.3.0的分布式集群搭建
- 一个IO的传奇一生 系列 存储之道
- 并行计算vs分布式计算
- php 中PHP_EOL使用
- Linux 常用命令汇总
- 采用Spring AOP+Log4j记录项目日志
- Java程序员的Golang入门指南(下)
- .NET CORE 中使用AutoMapper进行对象映射
- Asp.Net Core 轻松学-被低估的过滤器
- NIO小纪
- 【Social listening实操】从社交媒体传播和文本挖掘角度解读《欢乐颂2》
- 实操演练!MathType几个绝妙小技巧!
- Swift 开发语法
- Python print format() 格式化内置函数
- 小程序常用API介绍
- spark在yarn-cluster上面执行报错
热门文章
- k64 datasheet学习笔记31---External Bus Interface (FlexBus)
- 利用微信企业号的告警功能,联动检测ICMP的shell脚本
- git与eclipse集成之文件回退
- office xml 方式
- FFmpeg Commits on May 30, 2017 remove libschroedinger &; libnut
- thinkphp中的内置操作数据库与mysql中的函数汇总
- python-序列化模块
- Codeforces 487E Tourists [广义圆方树,树链剖分,线段树]
- Confluence 6 管理协同编辑
- Confluence 6 编辑自定义 Decorators