10.17
把
inline bool compareisbn(const Sales_data &lhs, const Sales_data rhs)
{
return lhs.isbn() < rhs.isbn();
}
sort(sds.begin(), sds.end(),compareisbn);
改为
sort(sds.begin(), sds.end(), [] (const Sales_data &lhs, const Sales_data &rhs) {return lhs.isbn() > rhs.isbn();});
10.18
void bigies(vector<string> words, vector<string>::size_type sz)
{
elimdups(words); //将words里的单词按字典序排列,删除重复单词
// 按长度排序,长度相同的单词维持字典序
stable_sort(words.begin(), words.end(), [] (const string &a, const string &b) {return a.size < b.size();});
//获取一个迭代器,指向满足size() >= sz的元素
auto wc = find_if(words.begin(), words.end(), [sz] (const string &a) {return a.size() > sz;});
//计算满足size >= sz的元素的数目
auto count = words.end() - wc;
cout << count <<" "<< make_plural(count, "word", "s") << " of length " << sz << " or lenger " << endl;
//打印长度大于等于给定值的单词,每个单词后接一个空格
for_each(wc. words.end(), [] (const string &s) { cout << s << " ";});
cout << endl;
}
---------------------
作者:天上地下,唯我独尊
来源:CSDN
原文:https://blog.csdn.net/weixin_43333890/article/details/83185383
版权声明:本文为博主原创文章,转载请附上博文链接!
答案
find_if接受一对迭代器,表示一个范围,第三个参数是一个谓词。返回第一个使谓词返回非0值得元素,如果不存在这样的元素,返回为迭代器。
就本题而言,若使用find_if,要求序列已按字符串长度递增的顺序排好序。返回第一个长度>=sz的字符串的位置wc,则所有满足条件的字符串位于[wc, end)之间。
而partition不要求序列已排序,他对所有的字符串检查长度是否>=sz,将满足条件的字符串移动到序列前端,不满足条件的字符串都移动到满足条件的字符串之后,返回满足条件的范围的尾后迭代器。因此满足条件的字符串位于范围[begin, wz)之间。
10.19
将上一题程序中的partition换为stable_partition即可。