1. 程式人生 > >2018.10.21——10.3定製操作

2018.10.21——10.3定製操作

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即可。