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

使用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函数的转换的更多相关文章

  1. ORACLE常用数值函数、转换函数、字符串函数

    本文更多将会介绍三思在日常中经常会用到的,或者虽然很少用到,但是感觉挺有意思的一些函数.分二类介绍,分别是: 著名函数篇 -经常用到的函数 非著名函数篇-即虽然很少用到,但某些情况下却很实用 注:N表 ...

  2. js字符转换成整型 parseInt()函数规程Number()函数

    今天在做一个js加法的时候,忘记将字符转换成整型,导致将加号认为是连接符,  在运算前要先对字符井行类型转换,使用parseInt()函数   使用Number()将字符转换成int型效果更好

  3. sql server ,OVER(PARTITION BY)函数用法,开窗函数,over子句,over开窗函数

    https://technet.microsoft.com/zh-cn/library/ms189461(v=sql.105).aspx https://social.msdn.microsoft.c ...

  4. 剑指Offer28 最小的K个数(Partition函数应用+大顶堆)

    包含了Partition函数的多种用法 以及大顶堆操作 /*********************************************************************** ...

  5. Oracle的学习三:java连接Oracle、事务、内置函数、日期函数、转换函数、系统函数

    1.java程序操作Oracle java连接Oracle JDBC_ODBC桥连接 1.加载驱动: Class.forName("sun.jdbc.odbc.JdbcodbcDriver& ...

  6. matlab——sparse函数和full函数(稀疏矩阵和非稀疏矩阵转换)

    函数功能:生成稀疏矩阵 使用方法 :S = sparse(A) 将矩阵A转化为稀疏矩阵形式,即矩阵A中任何0元素被去除,非零元素及其下标组成矩阵S.如果A本身是稀疏的,sparse(S)返回S. S ...

  7. 寻找序列中最小的第N个元素(partition函数实现)

    Partition为分割算法,用于将一个序列a[n]分为三部分:a[n]中大于某一元素x的部分,等于x的部分和小于x的部分. Partition程序如下: long Partition (long a ...

  8. sqlserver 自学笔记 函数实训 学分学期转换函数的设计

    设计目的: 1.运用sql基本知识,编写学期转换函数. 2.运用sql基本知识,编写学分转换函数,将考试成绩转换为学分 3.通过上述函数的编写与调试,熟练掌握 sql函数的编写.调试与使用方法. 设计 ...

  9. 快速排序 partition函数的所有版本比较

    partition函数是快排的核心部分 它的目的就是将数组划分为<=pivot和>pivot两部分,或者是<pivot和>=pivot 其实现方法大体有两种,单向扫描版本和双向 ...

随机推荐

  1. CDC 虚拟com口

    现在很多电脑已经不带232了,特别是手提电脑.这使很多使用手提在外调试人员非常不方便.或许你可以买一条市面上usb转232转换线,但这些线抗干扰不是太好,在一些干扰大的地方会发生连接中断的问题,所以往 ...

  2. Apache CXF 例子

    来自:http://www.cnblogs.com/frankliiu-java/articles/1641949.html Apache CXF 是一个开放源代码框架,是在Xfire 跟Celtix ...

  3. IPSEC实现

    IPSEC介绍与实现 一.介绍 IPSec 协议不是一个单独的协议,它给出了应用于IP层上网络数据安全的一整套体系结构,包括网络认证协议 Authentication Header(AH).封装安全载 ...

  4. BZOJ_[HNOI2008]_Cards_(置换+Burnside引理+乘法逆元+费马小定理+快速幂)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1004 共n个卡片,染成r,b,g三种颜色,每种颜色的个数有规定.给出一些置换,可以由置换得到的 ...

  5. 【转】基本数据持久性(一) 使用plist保存和读取数据

    原文网址:http://www.it165.net/pro/html/201309/7170.html 想保存成绩.记录得分.保存账号密码等等?数据持久性可以做到这一点!这篇文章通过简单的程序,来分享 ...

  6. 从头开始编写一个Orchard网上商店模块(5) - 创建和渲染ProductCatalog的内容类型

    原文地址: http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-pa ...

  7. Android 动画-alpha(渐变透明度动画效果)

    今天苦于思索应用如何美观,首先从载入页面的第一眼开始,Android动画分为四种:alpha(渐变透明度),scale(渐变尺寸伸缩),translate(画面转换位置移动),rotate(画面转移旋 ...

  8. MFC VS2005 添加Override 和 Message

    VS2005 1.Overrides OnInitDialog() 在Class View选中 这个类,然后properties中点Message 旁边的Overrides, 添加OnInitDial ...

  9. 如何使用eclipse进行嵌入式Linux的开发

    如何使用eclipse进行嵌入式Linux的开发 作者:曾宏安,华清远见嵌入式学院高级讲师. 如何使用eclipse进行嵌入式Linux的开发 习惯了在windows环境下开发的程序员在转到Linux ...

  10. STL总结之queue, priority_queue, stack

    之所以把这三个容器放在一起,是因为他们都是容器适配器.   STL中queue就是我们常用的FIFO队列,实现是一个容器适配器,这种数据结构在网络中经常使用.   queue的模板声明: templa ...