【C++ Primer (第五版)】 练习10.25遇到的问题

练习10.25:在10.3.2节(第349页)的练习中,编写了一个使用partition的biggies版本。使用check_size和bind重写此函数。

以下是自己写的代码:

#include "iostream"
#include "string"
#include "vector"
#include "algorithm"
#include "functional"                                                                                                        
#define n 5

using namespace std;
using namespace std::placeholders;
void elimDups(vector<string>& words);
string make_plural(size_t ctr, const string& word, const string& ending);
void biggies(vector<string>& words, vector<string>::size_type sz);
bool check_size(string word);

int main()
{
	vector<string> words = { "the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle" };
	biggies(words,n);
	system("pause");
	return 0;
}
//求大于等于一个给定长度的单词有多少。我们还会修改输出,使程序只打印大于等于给定长度的单词。
void biggies(vector<string>& words, vector<string>::size_type sz)
{
	elimDups(words);
	//auto wc = partition(words.begin(), words.end(), check_size);
	auto wc = partition(words.begin(), words.end(), bind(check_size, _1, words));
	auto count = wc-words.begin();
	cout << count << " " << make_plural(count, "word", "s")
		<< " of length " << sz << " or longer" << endl;
	for_each(words.begin(),wc, [](const string& s) {cout << s << " "; });
	cout << endl;
}

void elimDups(vector<string>& words)
{
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
}
string make_plural(size_t ctr, const string& word, const string& ending)
{
	return (ctr > 1) ? word + ending : word;
}
bool check_size(string word)
{
	return word.size() >= n;
}

调试后出现错误C388910.25
练习10.18中其他部分没有改动,只把

	auto wc = partition(words.begin(), words.end(), bind(check_size, _1, words));

改成了

		auto wc = partition(words.begin(), words.end(), check_size);

结果可以正常运行:
10.18

到底是什么问题?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值