c++ primer第五版(中文)习题答案 第十章第三节第四小节-参数绑定

本博客知识记录自己学习中的笔记或者记录,如果有错误欢迎大家纠正。
本节主要学习bind函数 理解的主要作用有

学习笔记:
bind定义在头文件 functional中 其作用主要为
1.改变函数参数个数
2.改变参数顺序
使用占位符_n(n为整数) 为对应的函数的参数顺序号。占位符在命名空间std::placeholders(在头文件functional中) 在使用的时候要注意域名空间
using std::placeholders::_1 然后调用_1;或者直接在函数中直接调用 std::placeholders::_1。

bind 调用的时候是对参数进行拷贝的方式进行
如果bind 的参数类型不能拷贝或者构造 的要使用ref或者cref进行包装才能使用。

习题解答
10.22 重写统计长度小于6的单词数量的程序。使用函数代替lambda。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

//make_plural(wc, "word ", "s ")当输入中文本中word数大于一是在word后加s,为words为word的复数!
std::string make_plural(size_t ctr, const std::string &word, const std::string &ending)
{
    return (ctr == 1) ? word : word + ending;
}
bool IsStringSizeBigSz(const std::string &s, std::string::size_type sz)
{
    return s.size() <= sz;
}
void biggies(std::vector<std::string> &words,
    std::vector<std::string>::size_type sz)
{
//使用bind注意占位符
    auto count = count_if(words.begin(), words.end(),
        bind(IsStringSizeBigSz, std::placeholders::_1, sz));

    std::cout << count << " " << make_plural(count, "word", "s")
        << " of length " << sz << " or longer " << std::endl;
    std::cout << std::endl;
}

int main()
{
    //删除了原先的排序函数

    std::string s = "";
    //注意在win下使用ctrl+z结束输入
    while (std::cin >> s)
        vecString.push_back(s);

    biggies(vecString, 6);


    system("pause");

    return 0;
}
输出结果为:

输出结果为:
这里写图片描述
10.23 bind接受几个参数。

在代码提示信息中可以知道占位符最多有20个即n<=20;
这里写图片描述

10.24 给定一个string,使用bind和check_size在int的vector中找第一个大于string长度的值

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>


bool check_size(const std::string &s, int sz)
{
    return s.size() < sz;
}

int main()
{
    std::vector<int>vecInt;
    std::string s = "ILoveLQ";
    int c = 0;
    while (std::cin >> c)
        vecInt.push_back(c);

    auto wc = find_if(vecInt.begin(), vecInt.end(), bind(check_size, s, std::placeholders::_1));

    std::cout << "the frist bigger string size : number is " << *wc << std::endl;
    system("pause");

    return 0;
}

输出结果为:
这里写图片描述

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

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
//将words按字典排序,删除重复单词
void elimDups(std::vector<std::string> &words)
{
    //排序
    sort(words.begin(), words.end());
    auto end_unique = unique(words.begin(), words.end());
    words.erase(end_unique, words.end());
}

//make_plural(wc, "word ", "s ")当输入中文本中word数大于一是在word后加s,为words为word的复数!
std::string make_plural(size_t ctr, const std::string &word, const std::string &ending)
{
    return (ctr == 1) ? word : word + ending;
}
bool check_size(const std::string &s, std::string::size_type sz)
{
    return s.size() < sz;
}
//原版
void biggies(std::vector<std::string> &words,
    std::vector<std::string>::size_type sz)
{
    elimDups(words);//将words按字典排序,删除重复单词

    //按长度排序,长度相同的单词维持字典排序
    stable_sort(words.begin(), words.end(),
        [](const std::string &a, const std::string &b)
    {return a.size() < b.size(); });


    /*partition的算法,它接收一个谓词,对容器内容精选划分,使得谓词为true的值会排在容器前半段,而使谓词为false的值会排在后半段,算法返回一个迭代器,指向最后一个使谓词为true 的元素之后的位置位置*/

    //获取一个迭代器,指向第一个满足size()>=sz的元素
    auto wc = partition(words.begin(), words.end(),bind(check_size,std::placeholders::_1,sz));//注意同时应该改变比较值 

    //计算满足size>=sz的元素的数目
    auto count = words.end() - wc;
    std::cout << count << " " << make_plural(count, "word", "s")
        << " of length " << sz << " or longer " << std::endl;

    //打印长度大于等于给定值的单词,每个单词后面节一个空格
    for_each(wc, words.end(),
        [](const std::string &s){std::cout << s << "  "; });

    std::cout << std::endl;

}

int main()
{

    std::vector<std::string>vecString;
    std::string s = "";

    //注意在win下使用ctrl+z结束输入 
    while (std::cin >> s)
    {
        vecString.push_back(s);
    }

    biggies(vecString, 4);



    system("pause");
    return 0;
}

输出结果为:
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值