问题
今天这个问题是对昨天问题的更新.[昨天问题]
- 重写方法,用partition代替find_if.
- 用stable_partition重写前一题的程序,与stable_sort类似,在划分后的序列中维持原有元素的顺序.
源代码
源码1:
/*************************************************************************
> File Name: lambda05.cpp
> Author: puruidong
> Mail: 1@w1520.com
> Created Time: 2014年06月13日
************************************************************************/
#include
#include
#include
using namespace std;
/****************************
编写自己的biggies程序.
--
使用partition代替find_if.
*************************************/
void bis(vector
{
sort(vec.begin(),vec.end());//排序.
auto resdel= unique(vec.begin(),vec.end());//删除重复单词
vec.erase(resdel,vec.end());
stable_sort(vec.begin(),vec.end());//按长度排序,长度相同的维持字典序.
auto reds = partition(vec.begin(),vec.end(),[sz](const string &s){return sz>s.size();});//获取一个迭代器,指向第一个满足size()>=sz的元素.
auto count = vec.end()-reds;
cout << "总共有:" << count << "个大于等于" << sz << "的元素" << endl;
cout << "传入的数值是:" << sz << "\n大于等于此值的元素如下:" << endl;
for_each(reds,vec.end(),[](const string &s){cout << s << " " << endl;});
} int main()
{
vector
string pa;
cout << "根据传入的数值输出vector中大于等于此数值的元素:" << endl;
while(cin >> pa)
{
vec.push_back(pa);
}
bis(vec,5);
return 0;
}
源码2:
/*************************************************************************
> File Name: lambda6.cpp
> Author: puruidong
> Mail: 1@w1520.com
> Created Time: 2014年06月13日 星期五 23时52分14秒
************************************************************************/
#include
#include
#include
using namespace std;
/****************************
编写自己的biggies程序.
--
使用stable_partition重写前一个部分.
*************************************/
void bis(vector
{
sort(vec.begin(),vec.end());//排序.
auto resdel= unique(vec.begin(),vec.end());//删除重复单词
vec.erase(resdel,vec.end());
stable_partition(vec.begin(),vec.end(),[sz](const string &s){return s.size()>=sz;});//按长度排序,长度相同的维持字典序.
auto reds = partition(vec.begin(),vec.end(),[sz](const string &s){return sz>s.size();});//获取一个迭代器,指向第一个满足size()>=sz的元素.
auto count = vec.end()-reds;
cout << "总共有:" << count << "个大于等于" << sz << "的元素" << endl;
cout << "传入的数值是:" << sz << "\n大于等于此值的元素如下:" << endl;
for_each(reds,vec.end(),[](const string &s){cout << s << " " << endl;});
} int main()
{
vector
string pa;
cout << "根据传入的数值输出vector中大于等于此数值的元素:" << endl;
while(cin >> pa)
{
vec.push_back(pa);
}
bis(vec,5);
return 0;
}