您的位置:首页 > 编程语言 > C语言/C++

C++ primer 第五版 中文版 练习 10.16 个人code

2014-09-09 22:42 543 查看
C++ primer 第五版 中文版 练习 10.16

题目:使用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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: