10.3.2 节练习

本文提供了一系列关于C++中Lambda表达式的练习示例,包括简单的加法操作、捕获变量进行运算以及利用Lambda实现字符串处理算法等。通过这些练习,读者可以更好地理解并掌握Lambda在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值