C++之仿函数

2023-11-06

最近再看STL源码的时候看到里面的实现用了大量的仿函数,然后上网搜集了一些关于仿函数的知识。

仿函数(Functor)又称为函数对象(Function Object)是一个能行使函数功能的类。仿函数的语法几乎和我们普通的函数调用一样,不过作为仿函数的类,都必须重载 operator() 运算符。因为调用仿函数,实际上就是通过类对象调用重载后的 operator() 运算符。

如果编程者要将某种“操作”当做算法的参数,一般有两种方法:
(1)一个办法就是先将该“操作”设计为一个函数,再将函数指针当做算法的一个参数。比如说我们定义一个排序函数,比较函数操作可以设计一个函数,利用函数指针作为参数传递给排序函数
(2)将该“操作”设计为一个仿函数(就语言层面而言是个 class),再以该仿函数产生一个对象,并以此对象作为算法的一个参数,接下来我们就详细讲一下如何设计仿函数。

如下是定义了一个比较仿函数:
 

class comp
{
public:
    explicit comp(int t):a(t){}//显式构造函数
    bool operator()(int num) const//const放前面表示这个函数的返回值是不可修改的,放后面表示这个函数不修改当前对象的成员。
    {
        return num>a;
    }
private:
    const int a;
};
int count(int *start,int *end,comp a)
{
    int *b=start;
    int c=0;
    while(b<=end)
    {
        c+=a(*b)?1:0;
        b++;
    }
    return c;
}
int main()
{
    comp a(10);
    cout<<a(11)<<endl;
    cout<<a(9)<<endl;
    int aa[4]={0,10,20,30};
    int result=count(aa,aa+3,a);
    cout<<result<<endl;
    return 0;
}

运行结果:

1
0
2
按 <RETURN> 来关闭窗口...

STL中涉及到了大量的仿函数,一般都用模板实现

1、STL中基础仿函数

1、仿函数定义自己型别
算法内部可能需要使用仿函数返回值或者输出值的类型参数,因此定义两个类。

//一元函数的参数和返回值类型,通常被继承
template <class Arg, class Result>
struct unary_function {
    typedef Arg argument_type;
    typedef Result result_type;
};

//二元函数的参数和返回值类型,通常被继承
template <class Arg1, class Arg2, class Result>
struct binary_function {
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
};  

上述两个类分别定义了一元和二元函数参数和返回值型别,对应的仿函数类仅仅需要继承此类即可。

2、算术类仿函数
标准库给我们定义了一些通用的仿函数,可以直接调用,生成对象,面向用户。

//算术类仿函数 + - * / %
//plus仿函数,生成一个对象,里面仅仅有一个函数重载的方法。
template <class T>
struct plus : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x + y; }
};

//minus仿函数
template <class T>
struct minus : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x - y; }
};

template <class T>
struct multiplies : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x * y; }
};

template <class T>
struct divides : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x / y; }
};
template <class T>
struct modulus : public binary_function<T, T, T> {
    T operator()(const T& x, const T& y) const { return x % y; }
};

//取负值
template <class T>
struct negate : public unary_function<T, T> {
    T operator()(const T& x) const { return -x; }
};

单独使用仿函数,通常将仿函数和算法部分单独分开使用。

#include <iostream>     // std::cout
#include <functional>   // std::plus
#include <algorithm>    // std::transform
using namespace std;
int main(void)
{
    cout << minus<int>()(10,5) << endl;//5
    cout << multiplies<int>()(10,5) << endl;//50
    cout << divides<int>()(10,5) << endl;//2
    cout << modulus<int>()(10,5) << endl;//0
    cout << negate<int>()(10) << endl;//-10
    return 0;
}

运行结果:

5
50
2
0
-10

3、关系运算符仿函数

//关系运算符仿函数
// x==y 仿函数
template <class T>
struct equal_to : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x == y; }
};

// x!=y 仿函数
template <class T>
struct not_equal_to : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x != y; }
};
// x>y 仿函数
template <class T>
struct greater : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x > y; }
};
// x<y 仿函数
template <class T>
struct less : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x < y; }
};

// x>=y 仿函数
template <class T>
struct greater_equal : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x >= y; }
};
// x<=y 仿函数
template <class T>
struct less_equal : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x <= y; }
};
// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
#include <functional>   // std::

bool myfunction (int i,int j) { return (i < j); }

int main () {
  int myints[] = {32,71,12,45,26,80,53,33};
  std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

  // using function as comp
  std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

  // using object as comp
  std::sort (myvector.begin(), myvector.end(), std::less<int>());     //(12 26 32 33 45 53 71 80)

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

 运行结果:

myvector contains:12 26 32 33 45 53 71 80

4、逻辑运算符仿函数

template <class T>
struct logical_and : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x && y; }
};

template <class T>
struct logical_or : public binary_function<T, T, bool> {
    bool operator()(const T& x, const T& y) const { return x || y; }
};

template <class T>
struct logical_not : public unary_function<T, bool> {
    bool operator()(const T& x) const { return !x; }
};

5、证同、选择、投射仿函数

///
//证同仿函数,主要用于RB或者hashmap里面 key = value情况
template <class T>
struct identity : public unary_function<T, T> {
  const T& operator()(const T& x) const { return x; }
};

//选择仿函数,主要用与RB和hashmap里面key 不为value情况,从pair种取出key
template <class Pair>
struct select1st : public unary_function<Pair, typename Pair::first_type> {
  const typename Pair::first_type& operator()(const Pair& x) const
  {
    return x.first;
  }
};

//选择仿函数,主要用与RB和hashmap里面key 不为value情况,从pair种取出value
template <class Pair>
struct select2nd : public unary_function<Pair, typename Pair::second_type> {
  const typename Pair::second_type& operator()(const Pair& x) const
  {
    return x.second;
  }
};

//投射函数,输入x和y返回x
template <class Arg1, class Arg2>
struct project1st : public binary_function<Arg1, Arg2, Arg1> {
  Arg1 operator()(const Arg1& x, const Arg2&) const { return x; }
};
//投射函数,输入x和y返回y
template <class Arg1, class Arg2>
struct project2nd : public binary_function<Arg1, Arg2, Arg2> {
  Arg2 operator()(const Arg1&, const Arg2& y) const { return y; }
};
/

3、STL中仿函数适配器

仿函数适配器是通过将上述仿函数重新配置成含有新功能的模板函数。

1、对仿函数返回值进行否定适配器
传入仿函数对象即可,和以前一样使用,仅仅包装了一下子而已。

//否定一元返回值
//模板参数传入仿函数类
template <class Predicate>
class unary_negate
  : public unary_function<typename Predicate::argument_type, bool> {
protected:
  Predicate pred;//对象
public:
  explicit unary_negate(const Predicate& x) : pred(x) {}
  bool operator()(const typename Predicate::argument_type& x) const {
    return !pred(x);//这里是调用的关键
  }
};
//辅助函数,使得我们方便使用unary_negate<Pred>
//传入对象,并返回临时对象。
template <class Predicate>
inline unary_negate<Predicate> not1(const Predicate& pred) {
  return unary_negate<Predicate>(pred);//返回临时对象
}
//辅助函数,识别传入对象,通过模板萃取其模板型别,然后声明模板声明临时对象并用传入对象初始化。
/
//否定二元返回值
template <class Predicate> 
class binary_negate 
  : public binary_function<typename Predicate::first_argument_type,
                           typename Predicate::second_argument_type,
                           bool> {
protected:
  Predicate pred;
public:
  explicit binary_negate(const Predicate& x) : pred(x) {}
  bool operator()(const typename Predicate::first_argument_type& x, 
                  const typename Predicate::second_argument_type& y) const {
    return !pred(x, y); 
  }
};
template <class Predicate>
inline binary_negate<Predicate> not2(const Predicate& pred) {
  return binary_negate<Predicate>(pred);
}
//not1 example
#include <iostream>     // std::cout
#include <functional>   // std::not1
#include <algorithm>    // std::count_if

struct IsOdd {
  bool operator() (const int& x) const {return x%2 == 1;}
  typedef int argument_type;
};//类

int main () {
  int values[] = {1,2,3,4,5};
  int cx = std::count_if( values, values+5, std::not1(IsOdd()) );//找出不是奇数的个数
  //IsOdd()产生临时对象a,not1返回临时对象并用a初始化。
  std::cout << "There are " << cx << " elements with even values.\n";
  return 0;
}

输出:
There are 2 elements with even values.

// not2 example
#include <iostream>     // std::cout
#include <functional>   // std::not2, std::equal_to
#include <algorithm>    // std::mismatch
#include <utility>      // std::pair

int main () {
  int foo[] = {10,20,30,40,50};
  int bar[] = {0,15,30,45,60};

  std::pair<int*,int*> firstmatch,firstmismatch;

  firstmismatch = std::mismatch (foo, foo+5, bar, std::equal_to<int>());//返回第一个不匹配数值

  firstmatch = std::mismatch (foo, foo+5, bar, std::not2(std::equal_to<int>()));//返回第一个匹配的数值

  std::cout << "First mismatch in bar is " << *firstmismatch.second << '\n';
  std::cout << "First match in bar is " << *firstmatch.second << '\n';
  return 0;
}

输出:
First mismatch in bar is 0
First match in bar is 30

2、将仿函数某个参数绑定为固定值的适配器

//绑定参数,将二元函数某个参数绑定为恒定值///

//Operation前面讲解的仿函数类
template <class Operation> 
class binder2nd
  : public unary_function<typename Operation::first_argument_type,
                          typename Operation::result_type> {
protected:
  Operation op;//仿函数对象
  typename Operation::second_argument_type value;//第二个参数类型
public:
  binder2nd(const Operation& x,
            const typename Operation::second_argument_type& y) 
      : op(x), value(y) {}//传入x是对象的引用,第二参数的引用
  typename Operation::result_type
  operator()(const typename Operation::first_argument_type& x) const {//传入x,底层调用op
    return op(x, value);//将value绑定为op的第二个参数 
  }
};

//辅助函数,辅助产生绑定好的对象 bind2nd
template <class Operation, class T>
inline binder2nd<Operation> bind2nd(const Operation& op, const T& x) {
  typedef typename Operation::second_argument_type arg2_type;
  return binder2nd<Operation>(op, arg2_type(x));//仅仅产生临时对象,可以传给模板函数
}



//第一个参数绑定起来
template <class Operation> 
class binder1st
  : public unary_function<typename Operation::second_argument_type,
                          typename Operation::result_type> {
protected:
  Operation op;//操作
  typename Operation::first_argument_type value;//第一个参数类型
public:
  binder1st(const Operation& x,
            const typename Operation::first_argument_type& y)
      : op(x), value(y) {}//构造
  typename Operation::result_type
  operator()(const typename Operation::second_argument_type& x) const {
    return op(value, x); 
  }
};

//辅助函数调用进行
template <class Operation, class T>
inline binder1st<Operation> bind1st(const Operation& op, const T& x) {
  typedef typename Operation::first_argument_type arg1_type;
  return binder1st<Operation>(op, arg1_type(x));
}
//
// binder2nd example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  int numbers[] = {10,-20,-30,40,-50};
  int cx;
  int cx1;
  binder2nd< less<int> > IsNegative (less<int>(),0);//将less<int>重新包装产生新的对象binder2nd
  cx = count_if (numbers,numbers+5 , IsNegative);//二者用法一样
  cx1 = count_if (numbers,numbers+5,bind2nd(less<int>() , 0));
  cout << "There are " << cx <<"  "<< cx1 << " negative elements.\n";
  return 0;
}

输出结果:

There are 3 3 negative elements.

3、将两个仿函数合并成一个仿函数的适配器

///用于函数合成/
//一元仿函数合成操作
//h(x) = f( g(x) )
template <class Operation1, class Operation2>
class unary_compose : public unary_function<typename Operation2::argument_type,
                                            typename Operation1::result_type> {
protected:
  Operation1 op1;
  Operation2 op2;
public:
  unary_compose(const Operation1& x, const Operation2& y) : op1(x), op2(y) {}
  typename Operation1::result_type
  operator()(const typename Operation2::argument_type& x) const {
    return op1(op2(x));//类似f(g(x))
  }
};

template <class Operation1, class Operation2>
inline unary_compose<Operation1, Operation2> compose1(const Operation1& op1, 
                                                      const Operation2& op2) {
  return unary_compose<Operation1, Operation2>(op1, op2);//返回临时对象
}

//二元仿函数合成操作
//h(x) = f( g1(x) , g2(x) )
template <class Operation1, class Operation2, class Operation3>
class binary_compose
  : public unary_function<typename Operation2::argument_type,
                          typename Operation1::result_type> {
protected:
  Operation1 op1;
  Operation2 op2;
  Operation3 op3;
public:
  binary_compose(const Operation1& x, const Operation2& y, 
                 const Operation3& z) : op1(x), op2(y), op3(z) { }
  typename Operation1::result_type
  operator()(const typename Operation2::argument_type& x) const {
    return op1(op2(x), op3(x));//返回临时对象
  }
};

template <class Operation1, class Operation2, class Operation3>
inline binary_compose<Operation1, Operation2, Operation3> 
compose2(const Operation1& op1, const Operation2& op2, const Operation3& op3) {
  return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3);
}

4、将函数指针合并成仿函数的适配器

///用于函数指针/

//将一元函数指针包装成仿函数
template <class Arg, class Result>
class pointer_to_unary_function : public unary_function<Arg, Result> {
protected:
  Result (*ptr)(Arg);//函数指针变量
public:
  pointer_to_unary_function() {}
  explicit pointer_to_unary_function(Result (*x)(Arg)) : ptr(x) {}
  Result operator()(Arg x) const { return ptr(x); }
};

template <class Arg, class Result>
inline pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg)) {
  return pointer_to_unary_function<Arg, Result>(x);//传入函数指针、一元参数类型、返回值类型,返回一个仿函数对象
}

/

//将二元函数指针包装成仿函数
template <class Arg1, class Arg2, class Result>
class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result> {
protected:
    Result (*ptr)(Arg1, Arg2);
public:
    pointer_to_binary_function() {}
    explicit pointer_to_binary_function(Result (*x)(Arg1, Arg2)) : ptr(x) {}
    Result operator()(Arg1 x, Arg2 y) const { return ptr(x, y); }
};

template <class Arg1, class Arg2, class Result>
inline pointer_to_binary_function<Arg1, Arg2, Result> 
ptr_fun(Result (*x)(Arg1, Arg2)) {
  return pointer_to_binary_function<Arg1, Arg2, Result>(x);//返回对象即可
}
// ptr_fun example
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstdlib>
#include <numeric>
using namespace std;

int main () {
  char* foo[] = {"10","20","30","40","50"};
  int bar[5];
  int sum;
  transform (foo, foo+5, bar, ptr_fun(atoi) );//将函数指针转换成仿函数对象,这里输入函数指针效果一样
  transform (foo, foo+5, bar, atoi );
  sum = accumulate (bar, bar+5, 0);
  cout << "sum = " << sum << endl;
  return 0;
}

输出:
sum = 150

5、将成员函数指针提取出来包装成仿函数适配器

//函数指针类别:返回S 无输入  通过指针调用
template <class S, class T>
class mem_fun_t : public unary_function<T*, S> {
public:
  explicit mem_fun_t(S (T::*pf)()) : f(pf) {}//初始化
  S operator()(T* p) const { return (p->*f)(); }//调用,p里面对应的函数
private:
  S (T::*f)();//这是一个变量,这个函数指针变量
};
//辅助函数,直接通过模板萃取相应的型别,然后声明相应的对象
template <class S, class T>
inline mem_fun_t<S,T> mem_fun( S (T::*f)() ) { 
  return mem_fun_t<S,T>(f);//返回仿函数临时对象,真的很牛逼哦抽象出来了
}
//小例子
size_type length() const 
{ return _M_string_length; }
mem_fun(&string::length);//传入函数指针,::优先级大于&
//s 是 size_type  T是string  f是length,通过模板萃取出这些型别,即可产生相应的对象。


//有一个参数,通过指针调用
template <class S, class T, class A>
class mem_fun1_t : public binary_function<T*, A, S> {
public:
  explicit mem_fun1_t(S (T::*pf)(A)) : f(pf) {}
  S operator()(T* p, A x) const { return (p->*f)(x); }
private:
  S (T::*f)(A);
};

template <class S, class T, class A>
class const_mem_fun1_t : public binary_function<const T*, A, S> {
public:
  explicit const_mem_fun1_t(S (T::*pf)(A) const) : f(pf) {}
  S operator()(const T* p, A x) const { return (p->*f)(x); }
private:
  S (T::*f)(A) const;
};
// mem_fun example
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

int main () {
  vector <string*> numbers;

  // populate vector of pointers:
  numbers.push_back ( new string ("one") );
  numbers.push_back ( new string ("two") );
  numbers.push_back ( new string ("three") );
  numbers.push_back ( new string ("four") );
  numbers.push_back ( new string ("five") );

  vector<int> lengths(numbers.size());//预先分配内存空间

  transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun(&string::length));

  for (int i=0; i<5; i++) {
    cout << *numbers[i] << " has " << lengths[i] << " letters.\n";
  }

  // deallocate strings:
  for (vector<string*>::iterator it = numbers.begin(); it!=numbers.end(); ++it)
    delete *it;

  return 0;
}

输出:
one has 3 letters.
two has 3 letters.
three has 5 letters.
four has 4 letters.
five has 4 letters.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++之仿函数 的相关文章

随机推荐

  • 关于PyCharm编程中报出ImportError: Missing optional dependency ‘openpyxl‘. Use pip or conda to ins错误的解决办法

    关于PyCharm编程中报出ImportError Missing optional dependency openpyxl Use pip or conda to ins错误的解决办法之一 本人遇到的问题在于没有引入相应的包 解决的详细办
  • 如何才算成功?如何才能成功?

    author skate time 2012 08 14 如何才算成功 如何才能成功 个人感悟 清楚自己 熟悉周围环境 精通大势所趋 把握时机 创造机会 审时度势 衡量取舍 平和心态 引文 感悟成功与成功者 http blog sina c
  • 第二十五讲:OSPF路由协议邻居认证配置

    在相同OSPF区域的路由器上启用身份验证的功能 只有经过身份验证的同一区域的路由器才能互相通告路由信息 这样做不但可以增加网络安全性 对OSPF重新配置时 不同口令可以配置在新口令和旧口令的路由器上 防止它们在一个共享的公共广播网络的情况下
  • [Codeforces] combinatorics (R1600) Part.6

    Codeforces combinatorics R1600 Part 6 题单 https codeforces com problemset tags combinatorics 1201 1600 1326C Permutation
  • 华为od 最长密码

    题目描述 小王在进行游戏大闯关 有一个关卡需要输入一个密码才能通过 密码获得的条件如下 在一个密码本中 每一页都有一个由26个小写字母组成的若干位密码 每一页的密码不同 需要从这个密码本中寻找这样一个最长的密码 从它的末尾开始依次去掉一位得
  • C语言中,%d和%u的故事

    有同学问 C语言中 d 和 u有什么区别 C语言课本中常出现u 但我们常用的却是d c语言中的 u是输入输出格式说明符 表示按unsigned int格式输入或输出数据 d 有符号10进制整数 i 有符号10进制整数 o 无符号8进制整数
  • 创建spring项目(Maven)

    首先创建一个空Java项目 项目名称自定义 配置项目jdk 配置maven file gt setting 点 号 添加模块 选择maven
  • docker swarm

    文章目录 1 docker swarm 常用命令 2 docker swarm 官网地址 3 初始化节点 3 1 初始化节点 3 2 生成 manager 或者 worker 令牌 3 3 查看 swarm 信息 3 4 查看节点信息 4
  • mysql 存储过程编写中踩的坑-----工作项目实战一

    如要转载请注明 https blog csdn net qwert789p article details 103627980 创作不易 且行且珍惜 1 项目前提 由于计费系统先上线 CRM项目后上线 前期计费系统有的建立客户账户 商品以及
  • dsadd user 命令详解

    From Internet 现在说下DSADD批量创建用户的方法 首先在使用DSADD之前先讲下LDAP协议 目录服务使用LDAP这个公用协议来查找和定位对象 LDAP可以描述对象在那个域 对象在那个OU 对象自己的名字 通常它的语法为 O
  • Appium自动化测试<三>

    本文接着Appium自动化测试 二 写 文章戳这里 夜神模拟器开启坐标 手机桌面 gt 工具 gt 设置 gt 开发者选项 gt 打开 显示点按操作反馈 指针位置 打开之后 我们鼠标滑动的时候就可以看到指定元素的位置坐标 一 Appium
  • chatgpt赋能python:Python:一门强大的编程语言

    Python 一门强大的编程语言 Python是一门面向对象 解释型的高级动态编程语言 广泛应用于Web开发 数据科学 人工智能 计算机网络等各个领域 在过去的10年中 Python经历了快速的发展 并成为了世界上最受欢迎的编程语言之一 P
  • 如何在Silverlight 控件中设置默认.xaml 页面

    每个Silverlight 项目可以有多个 xaml 文件 但每次只能看到一个 xaml 页面 当你在Visual Studio 中创建一个Silverlight 项目是 将创建一个默认的名字叫做 MainPage xaml 的 xaml
  • SolidWorks二次开发,工程图螺纹孔标注

  • Python使用tensorflow实现图像识别(猫狗大战)-01

    Python使用tensorflow实现图像识别 猫狗大战 01 import data py import tensorflow as tf import numpy as np import os 引入tensorflow numpy
  • 三种socket超时方法

    文章目录 概述 通过alarm定时器实现 通过select实现 通过SO RCVTIMEO和SO SNDTIMEO实现 概述 socket超时有三种处理办法 使用alarm定时器 设置一定的超时时间 通过产生SIGALRM中断信号实现soc
  • qt creator编译qt工程时报错:undefined reference to

    qt creator编译qt工程时报错 undefined reference to qt creator编译qt工程时报错 该错误指向了本地文件 且经过调研知道 该类错误一般都是链接问题 仔细查看一下pro和pri文件 发现SystemL
  • LeetCode 704. 二分查找 27. 移除元素

    文章目录 前言 LeetCode 704 二分查找 1 题目 2 思路 3 代码 LeetCode 27 移除元素 1 题目 2 思路 3 代码 总结 前言 为了巩固所学的知识 作者尝试着开始发布一些学习笔记类的博客 方便日后回顾 当然 如
  • 【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案

    这是 Flutter 问题系列第 72 篇 如果觉得有用的话 欢迎关注专栏 Flutter SDK 2 5 0 Dart SDK 2 14 Android Studio 2021 2 1 Pathc 1 一 问题描述 项目中要自定义相机页
  • C++之仿函数

    最近再看STL源码的时候看到里面的实现用了大量的仿函数 然后上网搜集了一些关于仿函数的知识 仿函数 Functor 又称为函数对象 Function Object 是一个能行使函数功能的类 仿函数的语法几乎和我们普通的函数调用一样 不过作为