C++ Primer(第5版) 练习 10.22
练习 10.22 重写统计长度小于等于6 的单词数量的程序,使用函数代替lambda。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
> File Name: ex10.22.cpp
> Author:
> Mail:
> Created Time: Sun 03 Mar 2024 11:34:50 AM CST
************************************************************************/
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
using namespace placeholders;
bool check_size(const string &s, string::size_type sz){
return s.size() <= sz;
}
int main(){
vector<string> words;
string str;
cout<<"Enter strings: ";
while(cin>>str){
words.push_back(str);
if(cin.get() == '\n'){
break;
}
}
stable_sort(words.begin(), words.end(), [](const string &a, const string &b) { return a.size() > b.size(); });
auto wc = find_if(words.begin(), words.end(), bind(check_size, _1, 6));
for_each(wc, words.end(), [](const string &s){ cout<<s<<" "; });
cout<<endl;
return 0;
}