练习10.22:重写统计长度小于等于6的单词数量的程序,使用函数代替lambda。
答:见练习10.22.cpp
练习10.23:bind接受几个参数?
答:不一定,至少接受一个参数绑定。详见 P356 bind的参数一节
练习10.24:给定一个string,使用bind和check_size在一个int的vector中查找第一个大于string长度的值。
答:见 练习10.24.cpp
练习10.25:在10.3.2节(第349页)的练习中,编写了一个使用partition的biggies版本。使用check_size和bind重写此函数。
答:见 练习10.25.cpp
练习10.22
/*
*练习10.22
*2015/8/20
*问题描述:练习10.22:重写统计长度小于等于6的单词数量的程序,使用函数代替lambda。
*说明:对bind的使用
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const string &s, string::size_type sz)
{
return s.size() <= sz;
}
int main()
{
vector<string> words = {"there","is","frog","living","in","the","lake"};
vector<string>::size_type sz = 6;
int cnt1 = count_if(words.begin(),words.end(),[sz](const string &s){return s.size() > sz ;}); //长度大于6的
cout << "total numbers of words is: " << cnt1 << endl;
auto check6 = bind(check_size,_1,sz);
int cnt = count_if(words.begin(), words.end(),check6);
cout << "total numbers of words is: " << cnt << endl;
return 0;
}
练习10.24
/*
*练习10.24
*2015/8/20
*问题描述:练习10.24:给定一个string,使用bind和check_size在一个int的vector中查找第一个大于string长度的值。
*说明:对于bind的理解,以及和比较对象的理解,比较的对象是什么类型,那么占位符就应该是哪个,check_size没有变,变得是bind的占位符
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
using namespace std::placeholders;
bool check_size(const string &s,string::size_type sz)
{
return sz > s.size();
}
int main()
{
vector<int> words = {1,2,3,4,5,6,7};
string word = "happy";
cout << "word is: " << word << " size is:" << word.size() << endl;
auto f=[]() {cout << "1 2 3 4 5 6 7";};
cout << "numbers : ";
f();
cout << endl;
auto check5 = bind(check_size,word,_1);
auto wc = find_if(words.begin(), words.end(),check5);
cout << "First number which is bigger than s.size() is:" << endl;
cout << *wc << endl;
return 0;
}
练习10.25
/*
*练习10.25
*2015/8/20
*问题描述:练习10.25:在10.3.2节(第349页)的练习中,编写了一个使用partition的biggies版本。使用check_size和bind重写此函数。
*说明:学会用bind这一特性,在练习10.19基础上修改
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*/
/*
*练习10.19
*2015/8/18
*问题描述:练习10.19:用stable_partition重写前一题的程序,与stable_sort类似,在划分后的序列中维持原有元素的顺序。
*说明:在练习10.18的基础上改动,与原来程序不同的地方,stable_partition后,单词顺序和之前一致
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*/
/*
*练习10.18
*2015/8/18
*问题描述:练习10.18:重写biggies,用partition代替find_if。我们在10.3.1节练习10.13(第345页)中介绍了partition算法。
*说明:在练习10.16的基础上改动
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*/
/*
*练习10.16
*2015/8/18
*问题描述:练习10.16:使用lambda编写你自己版本的biggies。
*说明:复习前面elimDups的写法,make_plural参考了201页写法,书上说得很清楚
*作者:Nick Feng
*邮箱:nickgreen23@163.com
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
using namespace std;
using namespace std::placeholders;
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
void elimDups(vector<string> words)
{
stable_sort(words.begin(), words.end(),isShorter);
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
bool check_size(const string &s, string::size_type sz)
{
return s.size() < sz;
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr > 1) ? word + ending : word;
}
void biggies(vector<string> &words, vector<string>::size_type sz)
{
elimDups(words);
stable_sort(words.begin(), words.end(),[](const string &a, const string &b){return a.size() < b.size();});
//auto wc = find_if(words.begin(), words.end(),[sz](const string &a){return a.size() >= sz;});
//auto wc = partition(words.begin(), words.end(),[sz](const string &a){return a.size() < sz;});//使用partition代替find_if函数
//auto wc = stable_partition(words.begin(), words.end(),[sz](const string &a){return a.size() < sz;});//使用stable_partition代替partition
auto check = bind(check_size,_1,sz);//2015/8/20改动 使用bind check_size
auto wc = stable_partition(words.begin(), words.end(),check);
auto count = words.end() - wc;
cout << count << " " << make_plural(count,"word", "s") << " of length " << sz << " or longer" << endl;
for_each(wc, words.end(),[](const string &s){cout << s << " ";});
cout << endl;
}
int main()
{
vector<string> words = {"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
vector<string>::size_type sz = 5;
biggies(words,sz);
return 0;
}