【C++】仿函数、lambda表达式、包装器

news/2024/9/20 22:16:12/

1.仿函数

仿函数是什么?仿函数就是类中的成员函数,这个成员函数可以让对象模仿函数调用的行为。

  • 函数调用的行为:函数名(函数参数)
  • C++中可以让类实现:函数名(函数参数)调用函数

自己写一个仿函数:

  • 重载()运算符
class Sum
{
public:int operator()(int a, int b){return a + b;}
};

1.1 operator()的调用方式

成员函数operator()由三种调用方式:

函数重载的显示调用、重载的隐式调用、类名()仿函数调用。

int main()
{Sum object;cout << object.operator()(1, 3) << endl; // 重载函数的显示调用cout << object(1, 4) << endl; // 重载的隐式调用cout << Sum()(1, 5) << endl;// 类名() 这种形态的调用叫仿函数return 0;
}

接触比较多的仿函数是两个排序准则:greater<>(),less<>()。


1.2 greater<>()和less<>()的使用

C++中有sort函数,实现在algorithm中,库中的sort函数默认是升序的;

可以先看一下sort的原型:

template <class _RanIt>
_CONSTEXPR20 void sort(const _RanIt _First, const _RanIt _Last) { // order [_First, _Last)_STD sort(_First, _Last, less<>{});
}

sort的最后一个参数,传递的是一个仿函数,默认是less<>()。

void Print(vector<int> v)
{for (auto e : v){cout << e << " ";}cout << endl;
}
int main()
{vector<int> v{ 8, 1, 5, 0, 9 };sort(v.begin(), v.end());Print(v);return 0;
}

打印函数Print需要多次使用,多以进行封装。

如果不使用默认的less<>(),使用greater<>(),是不是就是降序排列?

int main()
{vector<int> v{ 8, 1, 5, 0, 9 };sort(v.begin(), v.end(), greater<int>());Print(v);return 0;
}

那知道了仿函数的原理,我们也可以自己手写一个仿函数:

template <class T>
class Compare
{
public:bool operator()(T a, T b){return a < b;// 左边小,右边大,为升序}
};
int main()
{vector<int> v{ 8, 1, 5, 0, 9 };sort(v.begin(), v.end(), Compare<int>());Print(v);return 0;
}

2.lambda表达式

2.1 基本语法

lambda表达式书写格式: [捕捉列表] (参数) -> 返回值{ statement }

  • 返回值一般都不写,编译器会自动推导

我们先来写一个简单的lambda:

// 交换的lambda
int main()
{int x = 0, y = 1;auto swap = [](int& x, int& y) {int tmp = x; x = y; y = tmp; };swap(x, y);cout << x << " " << y << endl;return 0;
}

这里我们并没有使用到捕捉列表,其实使用捕捉列表会更加简单:

int main()
{int x = 0, y = 1;auto swap = [&x, &y]{int tmp = x; x = y; y = tmp; };swap();cout << x << " " << y << endl;return 0;
}

2.2 lambda表达式与仿函数

观察下面一段程序:

struct Goods
{string _name; // 名字double _price; // 价格int _evaluate; // 评价Goods(const char* str, double price, int evaluate):_name(str), _price(price), _evaluate(evaluate){}
};
struct ComparePriceLess
{bool operator()(const Goods& gl, const Goods& gr){return gl._price < gr._price;}
};
struct ComparePriceGreater
{bool operator()(const Goods& gl, const Goods& gr){return gl._price > gr._price;}
};
int main()
{vector<Goods> v = { { "苹果", 2.1, 5 }, { "香蕉", 3, 4 }, { "橙子", 2.2,3 }, { "菠萝", 1.5, 4 } };sort(v.begin(), v.end(), ComparePriceLess());sort(v.begin(), v.end(), ComparePriceGreater());
}

按照商品的名字、价格、评价排序,而且可以从小到大排,也可以从大到小排,所以有六种排序方式;如果给sort传递仿函数,那么需要我们写六个类,重载operator()。

使用lambda表达式的话就很容易解决:

int main()
{vector<Goods> v = { { "苹果", 2.1, 5 }, { "香蕉", 3, 4 }, { "橙子", 2.2,3 }, { "菠萝", 1.5, 4 } };// 按名字排序sort(v.begin(), v.end(), [](const Goods g1, const Goods g2) {return g1._name < g2._name; });sort(v.begin(), v.end(), [](const Goods g1, const Goods g2) {return g1._name > g2._name; });//按价格排序sort(v.begin(), v.end(), [](const Goods g1, const Goods g2) {return g1._price < g2._price; });sort(v.begin(), v.end(), [](const Goods g1, const Goods g2) {return g1._price > g2._price; });// 按评价排序sort(v.begin(), v.end(), [](const Goods g1, const Goods g2) {return g1._evaluate < g2._evaluate; });sort(v.begin(), v.end(), [](const Goods g1, const Goods g2) {return g1._evaluate > g2._evaluate; });
}

3.包装器

3.1 function

C++中的function本质是一个类模板,也是一个包装器。

那么我们来看看,我们为什么需要function呢?

ret = func(x);
// 上面func可能是什么呢?那么func可能是函数名?函数指针?函数对象(仿函数对象)?也有可能
是lamber表达式对象?所以这些都是可调用的类型!如此丰富的类型,可能会导致模板的效率低下!
为什么呢?我们继续往下看
template<class F, class T>
T useF(F f, T x)
{static int count = 0;cout << "count:" << ++count << endl;cout << "count:" << &count << endl;return f(x);
}
double f(double i)
{return i / 2;
}
struct Functor
{double operator()(double d){return d / 3;}
};
int main()
{// 函数名cout << useF(f, 11.11) << endl;// 函数对象cout << useF(Functor(), 11.11) << endl;// lamber表达式cout << useF([](double d)->double{ return d/4; }, 11.11) << endl;return 0;
}

count的地址不同,说明useF函数模板实例化了三份,效率降低。

使用包装器解决上面的问题。


std::function在头文件<functional>
// 类模板原型如下
template <class T> function;     // undefined
template <class Ret, class... Args>
class function<Ret(Args...)>;
模板参数说明:
Ret: 被调用函数的返回类型
Args…:被调用函数的形参
#include <functional>
int f(int a, int b)
{return a + b;
}
struct Functor
{
public:int operator() (int a, int b){return a + b;}
};
class Plus
{
public:static int plusi(int a, int b){return a + b;}double plusd(double a, double b){return a + b;}
};
int main()
{// 函数名(函数指针)std::function<int(int, int)> func1 = f;cout << func1(1, 2) << endl;// 函数对象std::function<int(int, int)> func2 = Functor();cout << func2(1, 2) << endl;// lamber表达式std::function<int(int, int)> func3 = [](const int a, const int b){return a + b; };cout << func3(1, 2) << endl;// 类的成员函数std::function<int(int, int)> func4 = Plus::plusi;cout << func4(1, 2) << endl;std::function<double(Plus, double, double)> func5 = &Plus::plusd;cout << func5(Plus(), 1.1, 2.2) << endl;//非静态成员函数的指针需要类对象调用Plus()是一个匿名对象return 0;
}

下面看useF使用包装器后的效果:

template<class F, class T>
T useF(F f, T x)
{static int count = 0;cout << "count:" << ++count << endl;cout << "count:" << &count << endl;return f(x);
}
double f(double i)
{return i / 2;
}
struct Functor
{double operator()(double d){return d / 3;}
};
int main()
{std::function<double(double)> f1 = f;cout << useF(f1, 11.11) << endl;std::function<double(double)> f2 = Functor();cout << useF(f2, 11.11) << endl;std::function<double(double)> f3 = [](double d)->double { return d / 4; };cout << useF(f3, 11.11) << endl;return 0;
}

观察可以看出,使用包装器后useF只实例化出了一份。


3.2 bind

std::bind函数定义在头文件中,是一个函数模板,它就像一个函数包装器接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。一般而 言,我们用它可以把一个原本接收N个参数的函数fn,通过绑定一些参数,返回一个接收M个参数的新函数。同时,使用std::bind函数还可以实现参数顺序调整等操作。

函数原型:

template <class Fn, class... Args>
/* unspecified */ bind (Fn&& fn, Args&&... args);
// with return type (2) 
template <class Ret, class Fn, class... Args>
/* unspecified */ bind (Fn&& fn, Args&&... args);

arg_list中的参数可能包含形如_n的名字,其中n是一个整数,这些参数是“占位符”,表示

newCallable的参数,它们占据了传递给newCallable的参数的“位置”。数值n表示生成的可调用对

象中参数的位置:_1为newCallable的第一个参数,_2为第二个参数,以此类推。

下面举一个例子来更好的理解bind:

void print(int a, int b, int c)
{cout << a << " " << b << " " << c << endl;
}
int main()
{auto foo1 = std::bind(print, 1, 2, 3);foo1();auto foo2 = std::bind(print,1, std::placeholders::_1, std::placeholders::_2);// _1和_2是有顺序的foo2(1, 3);// 1 1 3foo2(3, 1);// 1 3 1return 0;
}


http://www.ppmy.cn/news/28297.html

相关文章

Qt中信号与槽的使用

Qt中信号与槽的使用 Qt当中一个重要的东西是信号和槽&#xff0c;它被用于对象之间的通信。 在Qt中&#xff0c;例如“点击按钮”这个事件就是发送信号的对象&#xff0c;接收信号的是某一个窗口&#xff0c;响应信号的是一个处理&#xff0c;可以是隐藏窗口或者是关闭窗口。…

Linux安装云原生网关Kong/KongA

目录1 概述2 创建服务器3 安装postgres4 安装kong5 安装node6 安装KONGA1 概述 Kong Kong是一款基于OpenResty&#xff08;NginxLua模块&#xff09;编写的高可用、易扩展的开源API网关&#xff0c;专为云原生和云混合架构而建&#xff0c;并针对微服务和分布式架构进行了特别…

ccc-台大林轩田机器学习基石-hw1

文章目录Question1-14Question15-PLAQuestion16-PLA平均迭代次数Question17-不同迭代系数的PLAQuestion18-Pocket_PLAQuestion19-PLA的错误率Question20-修改Pocket_PLA迭代次数Question1-14 对于有明确公式和定义的不需要使用到ml 智能系统在与环境的连续互动中学习最优行为策…

pytorch学习日记之图片的简单卷积、池化

导入图片并转化为张量 import torch import torch.nn as nn import matplotlib.pyplot as plt import numpy as np from PIL import Image mymi Image.open("pic/123.png") # 读取图像转化为灰度图片转化为numpy数组 myimgray np.array(mymi.convert("L"…

Linux系统之查看进程监听端口方法

Linux系统之查看进程监听端口方法一、端口监听介绍二、使用netstat命令1.netstat命令介绍2.netstat帮助3.安装netstat工具4.列出所有监听 tcp 端口5.显示TCP端口的统计信息6.查看某个服务监听端口三、使用ss命令1.ss命令介绍2.ss命令帮助3.查看某个服务监听端口四、使用lsof命令…

C#协变逆变

文章目录协变协变接口的实现逆变里氏替换原则协变 协变概念令人费解&#xff0c;多半是取名或者翻译的锅&#xff0c;其实是很容易理解的。 比如大街上有一只狗&#xff0c;我说大家快看&#xff0c;这有一只动物&#xff01;这个非常自然&#xff0c;虽然动物并不严格等于狗…

TypeScript算法基础——TS字符串的常用操作总结:substring、indexOf、slice、replace等

字符串的操作是算法题当中经常碰见的一类题目&#xff0c;主要考察对string类型的处理和运用。 在处理字符串的时候&#xff0c;我们经常会碰到求字符串长度、匹配子字符串、替换字符串内容、连接字符串、提取字符串字符等操作&#xff0c;那么调用一些简单好用的api可以让工作…

JavaWeb中异步交互的关键——Ajax

文章目录1,Ajax 概述1.1 作用1.2 同步和异步1.3 案例1.3.1 分析1.3.2 后端实现1.3.3 前端实现2&#xff0c;axios2.1 基本使用2.2 快速入门2.2.1 后端实现2.2.2 前端实现2.3 请求方法别名3&#xff0c;JSON3.1 概述3.2 JSON 基础语法3.2.1 定义格式3.2.2 代码演示3.2.3 发送异步…