练习10.20:
#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
void biggies(vector<string>& v, vector<string>::size_type sz)
{
auto f1 = [=](const string& c) {return c.size() > sz; };
auto count = count_if(v.begin(), v.end(), f1);
cout << count << endl;
}
int main()
{
vector<string>vs;
ifstream in("story.txt");
string word;
while (in >> word)
{
vs.push_back(word);
}
biggies(vs, 6);
system("pause");
return 0;
}
练习10.21:
#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
void IntLambda(int a)
{
int t = a;
//判断是否递减为0
auto f1 = [&a]()->bool {if (a > 0) { --a; return false; } else return true; };
//调用lambda,递减a
for (auto i = 0; i < t + 10; ++i)
{
cout << f1() << " ";
}
cout << endl;
}
int main()
{
IntLambda(6);
system("pause");
return 0;
}