C++Primer第五版 10.3.3节练习

练习10.20:标准库定义了一个名为count_if的算法。类似find_if,此函数接受一对迭代器,表示一个输入范围,还接受一个谓词,会对输入范围中每个元素执行。count_if返回一个计数值,表示谓词有多少次为真。使用count_if重写我们程序中统计有多少单词长度超过6的部分。

练习10.21:编写一个lambda,捕获一个局部int变量,并递减变量值,直至它变为0。一旦变量为0,再调用lambda应该不再递减变量。Lambda应该返回一个bool值,指出捕获的变量是否为0。

答:见练习10.20.cpp 练习10.21.cpp

练习10.20

/*
*练习10.20 
*2015/8/19 
*问题描述:练习10.20:标准库定义了一个名为count_if的算法。类似find_if,此函数接受一对迭代器,表示一个输入范围,还接受一个谓词,会对输入范围中每个元素执行。Count_if返回一个计数值,表示谓词有多少次为真。使用count_if重写我们程序中统计有多少单词长度超过6的部分。
*说明:在练习10.19的基础上改动, 注释掉//auto count = words.end() - wc; 新增  auto count = count_if(words.begin(), words.end(),[sz](const string &a){return a.size() >= sz;});//修改部分 按照题目要求 2015/8/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>

using namespace std;

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

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 count = words.end() - wc;
    auto count = count_if(words.begin(), words.end(),[sz](const string &a){return a.size() >= sz;});//修改部分 按照题目要求 2015/8/19 
    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 = 6;
    biggies(words,sz);
    return 0;
}

练习10.21

/*
*练习10.21 
*问题描述:练习10.21:编写一个lambda,捕获一个局部int变量,并递减变量值,直至它变为0。一旦变量为0,再调用lambda应该不再递减变量。Lambda应该返回一个bool值,指出捕获的变量是否为0。 
*说明:熟悉lambda表达式
*作者:Nick Feng
*邮箱:nickgreen23@163.com 
*/

#include <iostream>

using namespace std;

int main()
{
    int v1 = 10;
    auto f=[v1]()mutable{while(--v1);if(v1 == 0) return true; else return false;};
    if(f() == true)
        cout << "v1 is 0 now! " << endl;
    else
        cout << "oh, that couldn't happen! " << endl;
    return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值