C++ primer 第五版 中文版 练习 10.16 个人code
2014-09-09 22:42
543 查看
C++ primer 第五版 中文版 练习 10.16
题目:使用lambda编写你自己版本的biggies。
答:
题目:使用lambda编写你自己版本的biggies。
答:
/* 使用lambda编写你自己版本的biggies。 */ #include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std; bool isShorter(const string &s1, const string &s2) { return s1.size() < s2.size(); } void elimDups(vector<string> &words) { sort(words.begin(), words.end()); cout << "vector用sort重排后的元素内容为:"; for (auto a : words) cout << a << " "; cout << endl; auto end_unique = unique(words.begin(), words.end()); cout << "vector用unique重排后的元素内容为:"; for (auto a : words) cout << a << " "; cout << endl; words.erase(end_unique, words.end()); cout << "vector中删除重复元素后的内容为:"; for (auto a : words) cout << a << " "; cout << endl; } string make_plural(size_t ctr, const string &word, const string &ending) { return (ctr > 1) ? word + ending : word; } 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 = find_if(words.begin(), words.end(), [sz](const string &a){return a.size() >= sz; }); auto count = words.end() - wc; cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl; for_each(wc, words.end(), [](const string &s){cout << s << " "; }); cout << endl; } int main() { vector<string> svect = { "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" }; biggies(svect, 4); return 0; }
相关文章推荐
- C++ primer 第五版 中文版 练习 9.43 个人code+github上的代码
- C++ primer 第五版 中文版 练习 10.29 个人code
- C++ primer 第五版 中文版 练习 11.12 个人code
- C++ primer 第五版 中文版 练习 11.23 个人code
- C++ Primer 第五版 中文版 练习 12.14 and 12.15 个人code
- C++ primer 第五版 中文版 练习 10.31 个人code
- C++ primer 第五版 中文版 练习 11.14 个人code
- C++ primer 第五版 中文版 练习 11.31 个人code
- C++ primer 第五版 中文版 练习 9.49 个人code
- C++ primer 第五版 中文版 练习 10.13 个人code
- C++ primer 第五版 中文版 练习 10.18 个人code
- C++ primer 第五版 中文版 练习 10.30 个人code
- C++ primer 第五版 中文版 11.3.1 节练习 个人code
- C++ primer 第五版 中文版 练习 12.20 个人code
- C++ primer 第五版 中文版 练习 9.3 & 练习 9.4 个人 code
- C++ primer 第五版 中文版 练习 10.14 个人code
- C++ primer 第五版 中文版 练习 10.21 个人code
- C++ primer 第五版 中文版 练习 12.23 个人code
- C++ primer 第五版 中文版 练习 9.50 个人code
- C++ primer 第五版 中文版 练习 10.15 个人code