练习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;
}
调试后出现错误C3889
练习10.18中其他部分没有改动,只把
auto wc = partition(words.begin(), words.end(), bind(check_size, _1, words));
改成了
auto wc = partition(words.begin(), words.end(), check_size);
结果可以正常运行:
到底是什么问题?