练习 10.25:在 10.3.2 节(第 349 页)的练习中,编写了一个使用 partition 的 biggies 版本。使用 check_size 和 bind 重写此函数。
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace std::placeholders;
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
string IsPlural(const int &x)
{
return x > 1 ? " words" : " word";
}
bool check_size(const string &s, string::size_type sz)
{
return s.size() >= sz;
}
void biggies(vector<string> &words, vector<string>::size_type sz)
{
elimDups(words);
stable_sort(words.begin(), words.end(),
[] (const string &a, const string &b)
{ return a.size() < b.size();});
auto wc = partition(words.begin(), words.end(), bind(check_size, _1, sz));
auto count = wc - words.begin();
cout << count << IsPlural(count) << " of length " << sz << " or longer, they are: ";
for_each(words.begin(), wc, [] (const string &s) {cout << s << " ";});
cout << endl;
}
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
biggies(words, 5);
return 0;
}