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

2018.10.19——10.3定製操作

10.14

[] (const int &a, const int &b) {return a+b;}

答案

#include <iostream>

using namespace std;

int main(int argc, char *[])
{
	auto sum = [] (int a, int b) { return a+b;};
	cout << sum(1, 1) << endl;
	return 0;
}

10.15

include <iostream>

using namespace std;

int fuc(int a)
{
	auto sum = [a] (int b) {return a+b;};		//沒有給b賦值
	return sum;
}

int main()
{
	cout << fuc(1) << endl;
	return 0;
}

答案

#include <iostream>
using namespace std;
void add(int a)
{
	auto sum = [a] (int b) {return a + b;};
	cout << sum(1) << endl;
}
int main()
{
	add(1);
	add(2);
	return 0;
}

10.16

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;
}