当前位置: 首页 > article >正文

【C++ 面试 - 新特性】每日 3 题(五)

✍个人博客:Pandaconda-CSDN博客
📣专栏地址:http://t.csdnimg.cn/fYaBd
📚专栏简介:在这个专栏中,我将会分享 C++ 面试中常见的面试题给大家~
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

13. STL 中的绑定器是什么

  • bind1st:operator() 的第一个形参变量绑定成一个确定的值。
  • bind2nd:operator() 的第二个形参变量绑定成一个确定的值。
    应用实例:
#include <iostream>
#include <memory>
#include <thread>
#include <functional>
#include <algorithm>
#include <vector>

using namespace std;

template<typename Container>
void showContainer(Container& con)
{
    //类型还没有实例化 编译器不知道后面套的是类型还是变量
    //加上typename 告诉编译器后面的是类型
    typename Container::iterator it = con.begin(); 
    for (; it != con.end(); ++it) {
        cout << *it << ' ';
    }
    cout << endl;
}

int main()
{
    vector<int>vec;
    srand(time(nullptr));
    for (int i = 0; i < 20; i++) {
        vec.push_back(rand() % 100 + 1);
    }

    sort(vec.begin(), vec.end());
    showContainer(vec);

    sort(vec.begin(), vec.end(), greater<int>());
    showContainer(vec);

    //把70按序插入
    //库里只有二元的, 但是我们需要一元的
    //绑定器+二元函数对象 => 一元函数对象
    auto it1 = find_if(vec.begin(), vec.end(),
        bind1st(greater<int>(), 70));
    if (it1 != vec.end())
    {
        vec.insert(it1, 70);
    }
    else
        vec.push_back(70);
    showContainer(vec);

    return 0;
}

14. bind1st 和 bind2nd 的底层实现原理

封装了下,底层填充,变成二元。

#include <iostream>
#include <memory>
#include <thread>
#include <functional>
#include <algorithm>
#include <vector>

using namespace std;

template<typename Container>
void showContainer(Container& con)
{
    //类型还没有实例化 编译器不知道后面套的是类型还是变量
    //加上typename 告诉编译器后面的是类型
    typename Container::iterator it = con.begin(); 
    for (; it != con.end(); ++it) {
        cout << *it << ' ';
    }
    cout << endl;
}

template <typename Compare, typename T>
class _mybind1st
{
public:
    _mybind1st(Compare comp, T val)
        :_comp(comp), _val(val)
    { }
    bool operator()(const T& second)
    {
        return _comp(_val, second); //底层是二元函数对象
    }
private:
    Compare _comp;
    T _val;
};

template <typename Compare, typename T>
_mybind1st<Compare, T> mybind1st (Compare comp, const T& val)
{
    return _mybind1st<Compare, T>(comp, val); 
}

template<typename Iterator, typename Compare>
Iterator my_find_if(Iterator first, Iterator last, Compare comp)
{
    for (; first != last; ++first)
    {
        if (comp(*first))
        {
            return first;
        }
    }
    return last;
}

int main()
{
    vector<int>vec;
    srand(time(nullptr));
    for (int i = 0; i < 20; i++) {
        vec.push_back(rand() % 100 + 1);
    }

    sort(vec.begin(), vec.end());
    showContainer(vec);

    sort(vec.begin(), vec.end(), greater<int>());
    showContainer(vec);

    //把70按序插入
    //库里只有二元的, 但是我们需要一元的
    //绑定器+二元函数对象 => 一元函数对象
    auto it1 = my_find_if(vec.begin(), vec.end(),
            bind1st(greater<int>(), 70));
    if (it1 != vec.end())
    {
        vec.insert(it1, 70);
    }
    else
            vec.push_back(70);
    showContainer(vec);

    return 0;
}

15. bind 函数要如何使用?

#include <iostream>
#include <typeinfo>
#include <string>
#include <functional>

using namespace std;

void hello(string str) { cout << str << endl; }
int sum(int a, int b) { return a + b; }
class Test
{
public:
    int sum(int a, int b) { return a + b; }
};

int main()
{
    //bind 是函数模板,可以自动推演模板类型参数
    bind(hello, "hello")();
    cout << bind(sum, 10, 20)() << endl;
    cout << bind(&Test::sum, Test(), 20, 30)() << endl;

    //参数占位符 绑定器出了语句无法继续使用
    bind(hello, placeholders::_1)("测试占位符");
    cout << bind(sum, placeholders::_1, placeholders::_2)(10, 20) << endl;

    //此处把bind返回的绑定器复用起来了
    function<void(string)> func1 = bind(hello, placeholders::_1);
    func1("hello t1");
    func1("hello t2");

    return 0;
}
/*
hello
30
50
测试占位符
30
hello t1
hello t2
*/

http://www.kler.cn/news/293694.html

相关文章:

  • Mac 数据恢复技巧:恢复 Mac 上已删除的文件
  • 使用 Nacos 来管理微服务
  • 《软件架构基础》读书笔记
  • 【SQL】常见语句合集
  • 何时使用枚举处理前后端的数据传输
  • 深度学习(十一)-PaddlePaddle
  • Java语言程序设计基础篇_编程练习题**17.20 (二进制编辑器)
  • 手撕HashMap源码
  • SVN提交失败Can‘t create directory ‘E:\SVN\Tool\db\transactions\27-v.txn‘:
  • 【Oracle APEX开发小技巧 7】解决初始化数据在动态操作-变更中被识别跳出弹窗的问题
  • 【超详细】windows Docker安装
  • GDB:加载符号表
  • ubuntu22.04 qemu 安装windows on arm虚拟机
  • uniapp+vue3实现小程序和h5解压线上压缩包以及如何访问解压后的视频地址
  • 69-java 接口中可以有构造函数吗
  • 使用 VisionTransformer(VIT) FineTune 训练驾驶员行为状态识别模型
  • setTimeout设置为0和nexttick 谁先执行谁后执行
  • OXC:光交叉连接(optical cross-connect)-介绍
  • 计算机网络-VRRP基础概念
  • 第十七题:电话号码的字母组合
  • 上海市计算机学会竞赛平台2024年8月月赛丙组等差数列的素性
  • 数字图像处理基础:图像处理概念、步骤、方式介绍
  • 【区块链 + 人才服务】FISCO BCOS 高校实训和管理平台 | FISCO BCOS应用案例
  • 【Linux】自定义协议与序列化和反序列化
  • 热力图科普:数据可视化的利器
  • 68-java字符流和字节流
  • 【一嗨租车-注册安全分析报告-滑动验证加载不正常导致安全隐患】
  • DWG如何转换成PDF?总结了四种转换
  • excel比较两列差异性和一致性,统计之后降序排列
  • SQL 数据查询