find_if函数与partition函数的转换
2024-08-24 10:07:35
编写程序,求大于等于一个给定长度的单词有多少。我们还会修改输出,使程序只打印大于等于给定长度的单词。
使用find_if实现的代码如下:
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
void biggies(vector<string> &words,vector<string>::size_type sz)
{
sort(words.begin(),words.end());
auto end_unique=unique(words.begin(),words.end());
words.erase(end_unique,words.end());
stable_sort(words.begin(),words.end(),[](const string &s1,const string s2) {return s1.size()<s2.size();});
auto wc=find_if(words.begin(),words.end(),[sz](const string &s) { return s.size()>=sz;});
auto count=words.end()-wc;
cout<<count<<endl;
for_each(wc,words.end(),[](const string &s) {cout<<s<<" ";});
cout<<endl;
}
int main()
{
vector<string> words={"aaaaa","aaaaaaa","dfdaaaa","fdaa","aaa","dfaaaaa"};
biggies(words,);
return ;
}
使用partition代码的程序:
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
void biggies(vector<string> &words,vector<string>::size_type sz)
{
sort(words.begin(),words.end());
auto end_unique=unique(words.begin(),words.end());
words.erase(end_unique,words.end());
stable_sort(words.begin(),words.end(),[](const string &s1,const string s2) {return s1.size()<s2.size();});
auto wc=partition(words.begin(),words.end(),[sz](const string &s) { return s.size()>=sz;});
auto count=wc-words.begin();
cout<<count<<endl;
for_each(words.begin(),wc,[](const string &s) {cout<<s<<" ";});
cout<<endl;
}
int main()
{
vector<string> words={"aaaaa","aaaaaaa","dfdaaaa","fdaa","aaa","dfaaaaa"};
biggies(words,);
return ;
}
运行结果:
dfdaaaa dfaaaaa aaaaa aaaaaaa
当使用stable_partition后程序:
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
void biggies(vector<string> &words,vector<string>::size_type sz)
{
sort(words.begin(),words.end());
auto end_unique=unique(words.begin(),words.end());
words.erase(end_unique,words.end());
stable_sort(words.begin(),words.end(),[](const string &s1,const string s2) {return s1.size()<s2.size();});
for_each(words.begin(),words.end(),[](const string &s) {cout<<s<<" ";});
cout<<endl;
auto wc=stable_partition(words.begin(),words.end(),[sz](const string &s) { return s.size()>=sz;});
auto count=wc-words.begin();
cout<<count<<endl;
for_each(words.begin(),wc,[](const string &s) {cout<<s<<" ";});
cout<<endl;
}
int main()
{
vector<string> words={"aaaaa","aaaaaaa","dfdaaaa","fdaa","aaa","dfaaaaa"};
biggies(words,);
return ;
}
运行结果:
aaa fdaa aaaaa aaaaaaa dfaaaaa dfdaaaa aaaaa aaaaaaa dfaaaaa dfdaaaa
说明stable_partiton不改变字典顺序,是稳定的操作。
最新文章
- sqlyog导出json数据格式支持mysql数据转存mongodb
- kettle系列-4.kettle定制化开发工具类
- iOS Assigning to &#39;id<;XXXDelegate>;&#39; from incompatible type &#39;BViewController *__strong&#39;
- SQL学习笔记----更改SQL默认的端口号
- 微信小程序文件作用域模块引用
- 用CSS box-shadow画画
- linux backlog深入剖析以及netty设置backlog
- Base-Android快速开发框架(五)--网络操作之RequestModel、ResponeModel、CustomAsyncHttpClient
- android camera(三):camera V4L2 FIMC
- H面试程序(11): 判断字符串是否包含子串问题
- Markdown引用本地图片语法
- CentOS最小化安装后启用无线连接网络
- react组件之间的通信
- Android设备直接运行java项目?还杀不死?
- Java高阶回调,回调函数的另一种玩法
- Socket.IO学习之基础入门
- mysql学习笔记--数据库单表查询
- Lintcode452-Remove Linked List Elements-Easy
- CCProxy
- Nginx 服务器性能Bug和性能优化方案(真实经历)
热门文章
- Java二维数组
- 总结一下SQL语句中引号(&#39;)、quotedstr()、(&#39;&#39;)、format()在SQL语句中的用法
- Android HttpClient get传递数组
- URAL1009
- eclipse 使用指南
- TreeMap源码解析
- date命令--修改linux系统时间
- Codeforces Round #335 (Div. 2)B. Testing Robots解题报告
- WCF入门介绍
- HDU-3622 Bomb Game 2sat