C++函数适配器

简介: C++函数适配器

什么是函数适配器

函数适配,就是让函数指针调用的时候,函数后绑定特定的参数,从而让函数指针存在多种的调用形态。

bind函数基本用法

bind(),第一个参数是函数名,第二个参数可以是 std::placeholders::_1(表示占位第一个参数)–>参数不固定,可以通过传参,当然也有std :: placeholders::_2…

绑定普通函数

#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
int Max(int a, int b)
{
  return a > b ? a : b;
}
int main()
{
  //std :: placeholders ::_ 1 占位符
  auto p = bind(Max, std::placeholders::_1, 122);
  cout << p(1) << endl;  //第二个参数固定,只需要传第一个参数就行

绑定类中成员函数指针

1.bind(),第一个参数写类名限定的函数名,第二个参数&对象

2.学习使用函数指针

#include<iostream>
#include<functional>
#include<string>
#include<algorithm>
using namespace std;
class Test
{
public:
  void printDate(int a, int b, int c)
  {
    cout << a + b + c << endl;
  }
};
int main()
{
  //绑定类中成员函数指针
  Test test;
  auto fun = bind(&Test ::printDate, &test, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
  fun(1, 2, 3);
  //成员函数指针的使用
  //方法一:用auto推断
  auto fun1 = &Test::printDate;
  (test.*fun1)(1, 2, 3);
  //方法二:直接写
  void(Test :: * p1)(int a, int b, int c);
  p1 = &Test::printDate;
  (test.*p1)(1, 2, 8);
  void(Test:: * p2)(int a, int b, int c) = &Test::printDate;
  (test.*p2)(2, 4, 9);
  system("pause");
  return 0;
}

绑定仿函数

结合一些算法使用

#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
  vector<int> date = { 46, 89, 17, 89, 90 };
  auto fun = count_if(date.begin(), date.end(), bind(less<int>(), std::placeholders::_1, 60));
  cout << "小于60的人数" << fun << endl;
  system("pause");
  return 0;
}

结合包装器使用

include<iostream>
#include<functional>
#include<algorithm>
using namespace std;
void Date(int a, string b, double c)
{
  cout << "函数适配器和包装器的结合" << endl;
}
int main()
{
  //正常包装
  function<void(int, string, double)> fun2;
  fun2(1, "2", 1.1);
  //正常适配 (一个参数固定的话,这里类型也要写2个)
  function<void(int, string)> fun1 = bind(Date, std::placeholders::_1, std::placeholders::_2, 1.11);
  fun1(1, "3");
  //可以改变参数的位置
  function<void(string, int)> fun3 = bind(Date, std::placeholders::_2, std::placeholders::_1, 1.11);
  fun3("2", 1);
  //这里改变了第一个参数和第二个参数的位置,同时第三个参数固定
  return 0;
  system("pause");
}

函数适配器和包装器,在实际使用中可能很少用到,但我们必须要了解,学会使用。


相关文章
|
2天前
|
程序员 C++ 容器
在 C++中,realloc 函数返回 NULL 时,需要手动释放原来的内存吗?
在 C++ 中,当 realloc 函数返回 NULL 时,表示内存重新分配失败,但原内存块仍然有效,因此需要手动释放原来的内存,以避免内存泄漏。
|
13天前
|
存储 前端开发 C++
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
36 6
|
13天前
|
C++
C++ 多线程之线程管理函数
这篇文章介绍了C++中多线程编程的几个关键函数,包括获取线程ID的`get_id()`,延时函数`sleep_for()`,线程让步函数`yield()`,以及阻塞线程直到指定时间的`sleep_until()`。
16 0
C++ 多线程之线程管理函数
|
17天前
|
编译器 C语言 C++
C++入门3——类与对象2-2(类的6个默认成员函数)
C++入门3——类与对象2-2(类的6个默认成员函数)
22 3
|
17天前
|
编译器 C语言 C++
详解C/C++动态内存函数(malloc、free、calloc、realloc)
详解C/C++动态内存函数(malloc、free、calloc、realloc)
71 1
|
17天前
|
存储 编译器 C++
C++入门3——类与对象2-1(类的6个默认成员函数)
C++入门3——类与对象2-1(类的6个默认成员函数)
19 1
|
17天前
|
编译器 C语言 C++
C++入门6——模板(泛型编程、函数模板、类模板)
C++入门6——模板(泛型编程、函数模板、类模板)
29 0
C++入门6——模板(泛型编程、函数模板、类模板)
|
1月前
|
编译器 C++
【C++核心】函数的应用和提高详解
这篇文章详细讲解了C++函数的定义、调用、值传递、常见样式、声明、分文件编写以及函数提高的内容,包括函数默认参数、占位参数、重载等高级用法。
20 3
|
2月前
|
编译器 C++ 容器
【C++】String常见函数用法
【C++】String常见函数用法
|
2月前
|
存储 C++
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
35 0