莘县网站建设公司,淘宝标题优化网站,建设商城购物网站,企业软文怎么写1.仿函数
仿函数是什么#xff1f;仿函数就是类中的成员函数#xff0c;这个成员函数可以让对象模仿函数调用的行为。
函数调用的行为#xff1a;函数名(函数参数)C中可以让类实现#xff1a;函数名(函数参数)调用函数
自己写一个仿函数#xff1a;
重载()运算符
cla…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(vectorint v)
{for (auto e : v){cout e ;}cout endl;
}
int main()
{vectorint v{ 8, 1, 5, 0, 9 };sort(v.begin(), v.end());Print(v);return 0;
}打印函数Print需要多次使用多以进行封装。
如果不使用默认的less()使用greater()是不是就是降序排列
int main()
{vectorint v{ 8, 1, 5, 0, 9 };sort(v.begin(), v.end(), greaterint());Print(v);return 0;
}那知道了仿函数的原理我们也可以自己手写一个仿函数
template class T
class Compare
{
public:bool operator()(T a, T b){return a b;// 左边小右边大为升序}
};
int main()
{vectorint v{ 8, 1, 5, 0, 9 };sort(v.begin(), v.end(), Compareint());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()
{vectorGoods 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()
{vectorGoods 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表达式对象所以这些都是可调用的类型如此丰富的类型可能会导致模板的效率低下
为什么呢我们继续往下看
templateclass 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 functionRet(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::functionint(int, int) func1 f;cout func1(1, 2) endl;// 函数对象std::functionint(int, int) func2 Functor();cout func2(1, 2) endl;// lamber表达式std::functionint(int, int) func3 [](const int a, const int b){return a b; };cout func3(1, 2) endl;// 类的成员函数std::functionint(int, int) func4 Plus::plusi;cout func4(1, 2) endl;std::functiondouble(Plus, double, double) func5 Plus::plusd;cout func5(Plus(), 1.1, 2.2) endl;//非静态成员函数的指针需要类对象调用Plus()是一个匿名对象return 0;
}下面看useF使用包装器后的效果
templateclass 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::functiondouble(double) f1 f;cout useF(f1, 11.11) endl;std::functiondouble(double) f2 Functor();cout useF(f2, 11.11) endl;std::functiondouble(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;
}