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

【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);}elsevec.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);}elsevec.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.mrgr.cn/news/21312.html

相关文章:

  • Tensorflow2如何读取自制数据集并训练模型?-- Tensorflow自学笔记13
  • 第4章-02-用WebDriver驱动浏览器页面截图
  • GIS圈大事件!Cesium被收购了,是好是坏?
  • 智能制造中,叉车防撞报警系统的数字化管理
  • vivado 时钟交互报告
  • python画图|3D图基础教程
  • matplotlib中文乱码问题
  • SpringBoot 集成 PDFBox 实现电子签章
  • SQL的高级查询练习知识点下(day26)
  • 企业邮箱怎么设置邮箱监控(老板监控员工邮件的方法推荐)【企业管理必备】
  • 基于web宾馆管理系统设计与实现
  • C++学习, 类的构造函数
  • 【数据结构】你真的学会了二叉树了吗,来做一做二叉树的算法题及选择题
  • C++11 异常
  • VB 关键字
  • 2024 年高教社杯全国大学生数学建模竞赛 C 题 农作物的种植策略(详细思路+matlab代码+python代码+论文范例)
  • 【Linux修行路】线程同步——条件变量
  • 【抽代复习笔记】27-群(二十一):子群与生成子群
  • 图像处理基础篇-镜像仿射透视
  • C++学习,类的析构函数