编写程序,求大于等于一个给定长度的单词有多少。我们还会修改输出,使程序只打印大于等于给定长度的单词。

使用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不改变字典顺序,是稳定的操作。

最新文章

  1. sqlyog导出json数据格式支持mysql数据转存mongodb
  2. kettle系列-4.kettle定制化开发工具类
  3. iOS Assigning to &#39;id&lt;XXXDelegate&gt;&#39; from incompatible type &#39;BViewController *__strong&#39;
  4. SQL学习笔记----更改SQL默认的端口号
  5. 微信小程序文件作用域模块引用
  6. 用CSS box-shadow画画
  7. linux backlog深入剖析以及netty设置backlog
  8. Base-Android快速开发框架(五)--网络操作之RequestModel、ResponeModel、CustomAsyncHttpClient
  9. android camera(三):camera V4L2 FIMC
  10. H面试程序(11): 判断字符串是否包含子串问题
  11. Markdown引用本地图片语法
  12. CentOS最小化安装后启用无线连接网络
  13. react组件之间的通信
  14. Android设备直接运行java项目?还杀不死?
  15. Java高阶回调,回调函数的另一种玩法
  16. Socket.IO学习之基础入门
  17. mysql学习笔记--数据库单表查询
  18. Lintcode452-Remove Linked List Elements-Easy
  19. CCProxy
  20. Nginx 服务器性能Bug和性能优化方案(真实经历)

热门文章

  1. Java二维数组
  2. 总结一下SQL语句中引号(&#39;)、quotedstr()、(&#39;&#39;)、format()在SQL语句中的用法
  3. Android HttpClient get传递数组
  4. URAL1009
  5. eclipse 使用指南
  6. TreeMap源码解析
  7. date命令--修改linux系统时间
  8. Codeforces Round #335 (Div. 2)B. Testing Robots解题报告
  9. WCF入门介绍
  10. HDU-3622 Bomb Game 2sat