find_if函数与partition函数的转换
编写程序,求大于等于一个给定长度的单词有多少。我们还会修改输出,使程序只打印大于等于给定长度的单词。
使用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不改变字典顺序,是稳定的操作。
find_if函数与partition函数的转换的更多相关文章
- ORACLE常用数值函数、转换函数、字符串函数
本文更多将会介绍三思在日常中经常会用到的,或者虽然很少用到,但是感觉挺有意思的一些函数.分二类介绍,分别是: 著名函数篇 -经常用到的函数 非著名函数篇-即虽然很少用到,但某些情况下却很实用 注:N表 ...
- js字符转换成整型 parseInt()函数规程Number()函数
今天在做一个js加法的时候,忘记将字符转换成整型,导致将加号认为是连接符, 在运算前要先对字符井行类型转换,使用parseInt()函数 使用Number()将字符转换成int型效果更好
- sql server ,OVER(PARTITION BY)函数用法,开窗函数,over子句,over开窗函数
https://technet.microsoft.com/zh-cn/library/ms189461(v=sql.105).aspx https://social.msdn.microsoft.c ...
- 剑指Offer28 最小的K个数(Partition函数应用+大顶堆)
包含了Partition函数的多种用法 以及大顶堆操作 /*********************************************************************** ...
- Oracle的学习三:java连接Oracle、事务、内置函数、日期函数、转换函数、系统函数
1.java程序操作Oracle java连接Oracle JDBC_ODBC桥连接 1.加载驱动: Class.forName("sun.jdbc.odbc.JdbcodbcDriver& ...
- matlab——sparse函数和full函数(稀疏矩阵和非稀疏矩阵转换)
函数功能:生成稀疏矩阵 使用方法 :S = sparse(A) 将矩阵A转化为稀疏矩阵形式,即矩阵A中任何0元素被去除,非零元素及其下标组成矩阵S.如果A本身是稀疏的,sparse(S)返回S. S ...
- 寻找序列中最小的第N个元素(partition函数实现)
Partition为分割算法,用于将一个序列a[n]分为三部分:a[n]中大于某一元素x的部分,等于x的部分和小于x的部分. Partition程序如下: long Partition (long a ...
- sqlserver 自学笔记 函数实训 学分学期转换函数的设计
设计目的: 1.运用sql基本知识,编写学期转换函数. 2.运用sql基本知识,编写学分转换函数,将考试成绩转换为学分 3.通过上述函数的编写与调试,熟练掌握 sql函数的编写.调试与使用方法. 设计 ...
- 快速排序 partition函数的所有版本比较
partition函数是快排的核心部分 它的目的就是将数组划分为<=pivot和>pivot两部分,或者是<pivot和>=pivot 其实现方法大体有两种,单向扫描版本和双向 ...
随机推荐
- dojo 图表制作教程
http://www.sitepen.com/labs/code/charting/tutorial/tutorial1.html http://www.sitepen.com/labs/code/c ...
- c/c++ void 指针
原文 : http://blog.csdn.net/yyyuhan/article/details/3153290 1.概述 许多初学者对C/C++语言中的void及void指针类型不甚理解,因此在使 ...
- @Resource和@Autowired
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...
- bzoj1084
乍一看这题好难,根本不会: 直到看到1<=m<=2…… 显然分类讨论dp, 很快想到这题的dp和poj2430相类似 m=2的时候f[i,j,k]表示到第i行用了j个矩阵结尾状态为k时最大 ...
- 使用java远程调试技术监控代码运行
JAPA介绍 JPDA(Java Platform Debugger Architecture)是 Java 平台调试体系结构的缩写,通过 JPDA 提供的 API,开发人员可以方便灵活的搭建 Jav ...
- 【转】VS2010/MFC编程入门之二十五(常用控件:组合框控件Combo Box)
原文网址:http://www.jizhuomi.com/software/189.html 上一节鸡啄米讲了列表框控件ListBox的使用,本节主要讲解组合框控件Combo Box.组合框同样相当常 ...
- __cdecl、__stdcall、__fastcall、thiscall 进栈、出栈区别
https://en.wikipedia.org/wiki/X86_calling_conventions https://msdn.microsoft.com/en-us/library/984x0 ...
- 嵌入式 hi3518c裸板uboot烧写、kernel烧写、fs烧写小结
1.在uboot中我可以添加自己的命令,添加的方法是找到一个uboot的命令,然后模仿着去增加属于自己的命令代码以及实现函数就可以 2.记住在使用printf进行调试的时候,在遇到指针或者字符串的时候 ...
- 分割函数和根据Id串返回名字
需求:函数传入一个字符串参数 例如 123-456 将这个字符串123-456拆成两个值 123 456,在通过两个值分别查出数据(例如 张三 李四),拼接成 张三-李四 --声明变量 ...
- 我的第一个项目:用kinect录视频库
kinect深度视频去噪 kinectmod32.dll http://pan.baidu.com/s/1DsGqX 下载后改名kinect.dll 替换掉Redist\OpenNI2\Drivers ...