练习10.14 编写一个lambda,接受两个int,返回它们的和。
#include <iostream>
using namespace std;
int main()
{
int a = 7, b = 9;
auto sum = [a, b]() {return a + b; };
cout << sum() << endl;
return 0;
}
练习10.15 编写一个lambda,捕获它所在函数的Int,并接受一个Int参数,lambda应该返回捕获的int和int参数的和。
#include <iostream>
using namespace std;
int main()
{
int cat = 7;
auto sum = [cat](const int val) {return val + cat; };
cout << sum(5) << endl;
return 0;
}
练习10.16 使用lambda编写你自己版本的biggiers。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void elimDups(vector<string> &);
void biggies(vector<string> &, vector<string>::size_type);
string make_plural(const int, const string, const string);
int main()
{
vector <string> text;
string word;
while (cin >> word) {
text.push_back(word);
}
biggies(text, 3);
return 0;
}
void elimDups(vector<string> &text)
{
sort(text.begin(), text.end());
auto iter = unique(text.begin(), text.end());
text.erase(iter, text.end());
}
string make_plural(const int num, const string a, const string b)
{
return num > 1 ? a + b : a;
}
void biggies(vector<string> &text, vector<string>::size_type sz)
{
elimDups(text);
stable_sort(text.begin(), text.end(), [](const string &a, const string &b) {return a.size() < b.size(); });
auto iter = find_if(text.begin(), text.end(), [sz](const string &a) {return a.size() >= sz; });
auto cnt = text.end() - iter;
cout << cnt << make_plural(cnt, "word", "s") << " of length " << sz << " or longer" << endl;
for_each(iter, text.end(), [](const string word) {cout << word << " "; });
cout << endl;
}
练习10.17 重写10.3.1节练习10.12(第345页)的程序,在对sort的调用中使用lambda来代替函数compareIsbn。
补。
练习10.18 重写biggies,用partition代替find_if。我们在10.3.1节练习10.13(第345页)中介绍了partition算法。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void elimDups(vector<string> &);
void biggies(vector<string> &, vector<string>::size_type);
string make_plural(const int, const string, const string);
int main()
{
vector <string> text;
string word;
while (cin >> word) {
text.push_back(word);
}
biggies(text, 3);
return 0;
}
void elimDups(vector<string> &text)
{
sort(text.begin(), text.end());
auto iter = unique(text.begin(), text.end());
text.erase(iter, text.end());
}
string make_plural(const int num, const string a, const string b)
{
return num > 1 ? a + b : a;
}
void biggies(vector<string> &text, vector<string>::size_type sz)
{
elimDups(text);
stable_sort(text.begin(), text.end(), [](const string &a, const string &b) {return a.size() < b.size(); });
auto iter = partition(text.begin(), text.end(), [sz](const string &s) {return s.size() > sz; });
auto cnt = iter - text.begin();
cout << cnt << make_plural(cnt, "word", "s") << " of length " << sz << " or longer" << endl;
for_each(text.begin(), iter, [](const string word) {cout << word << " "; });
cout << endl;
}
练习 10.19 用stalbe_partition重写前一题的程序,与stable_sort类似,在划分后的序列中维持原有元素的顺序。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void elimDups(vector<string> &);
void biggies(vector<string> &, vector<string>::size_type);
string make_plural(const int, const string, const string);
int main()
{
vector <string> text;
string word;
while (cin >> word) {
text.push_back(word);
}
biggies(text, 3);
return 0;
}
void elimDups(vector<string> &text)
{
sort(text.begin(), text.end());
auto iter = un ique(text.begin(), text.end());
text.erase(iter, text.end());
}
string make_plural(const int num, const string a, const string b)
{
return num > 1 ? a + b : a;
}
void biggies(vector<string> &text, vector<string>::size_type sz)
{
elimDups(text);
auto iter = stable_partition(text.begin(), text.end(), [sz](const string &s) {return s.size() > sz; });
//auto iter = partition(text.begin(), text.end(), [sz](const string &s) {return s.size() > sz; });
auto cnt = iter - text.begin();
cout << cnt << make_plural(cnt, "word", "s") << " of length " << sz << " or longer" << endl;
for_each(text.begin(), iter, [](const string word) {cout << word << " "; });
cout << endl;
}