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

百度官方网站网址是多少备案核验单 网站类型

百度官方网站网址是多少,备案核验单 网站类型,建设黑彩网站需要什么,昆明网络推广哪里有1.仿函数 仿函数是什么?仿函数就是类中的成员函数,这个成员函数可以让对象模仿函数调用的行为。 函数调用的行为:函数名(函数参数)C中可以让类实现:函数名(函数参数)调用函数 自己写一个仿函数: 重载()运算符 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(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.yayakq.cn/news/68251/

相关文章:

  • 有个网站专做品牌 而且价格便宜湖南专业关键词优化
  • 开发公司需要什么资质seo和sem的关系
  • 制作网站程序杰奇小说网站建设
  • 品划网络做营销型网站wordpress注册弹出框
  • 三亚做民宿的都用什么网站网站开发名词解释
  • 无锡网站建设系统上海私人做网站
  • 燕郊网站建设哪家好网站建设中的风险
  • 台州网站优化公司虚拟主机可以建设什么网站
  • 用ps怎么做学校网站页面什么是黄页
  • 天长市城乡规划建设局网站wordpress onetone
  • 常州 网站制作网站建设厌倦
  • 长春火车站有几个wordpress 如何支持短代码
  • 装饰网站建设价格5118和百度指数
  • 网站建设合作协议深圳商业网站建设哪家
  • 程序员做彩票网站违法吗阿里云备案域名购买
  • 网站策划要遵循的原则云南九泰建设工程有限公司官方网站
  • 英德市住房和城乡建设局手机网站wordpress底部悬浮菜单
  • 淄博做网站跟优化网站建设如何控标
  • 网站上传好了如何做定向python 网站开发 普及
  • 网站建设公司湖南阿里云认证网站建设题库
  • wordpress云建站教程wordpress 登入 跳
  • 百度快照优化seo北京seo编辑
  • 刷网站软件宁波网站制作 收费
  • 电子商务网站建设 概念榆林华科网站建设
  • 如何做优化网站排名企业用什么做网站
  • 网站销户说明门户网站底部
  • 网站推广的10种方法wordpress透明
  • 大型大型网站建设方案ppt模板小白 wordpress
  • 柳州做网站那家好阿里云虚拟主机配置wordpress
  • 企业营销网站建设费用预算建个网站的电话号码