C++11 function类模板

std::function


1. std::function 简介
类模板std :: function是一个通用的多态函数包装器。 std :: function的实例可以存储,复制和调用任何可调用的目标 :包括函数,lambda表达式,绑定表达式或其他函数对象,以及指向成员函数和指向数据成员的指针。当std::function对象未包裹任何实际的可调用元素,调用该std::function对象将抛出std::bad_function_call异常。

2. function类模板

  template< class R, class... Args >
  class function<R(Args...)>;

模板参数说明:

  • R: 被调用函数的返回类型
  • Args…:被调用函数的形参

例如:function<int(int,int)> func;
则 function类的实例func可以指向 返回值为int型,有两个形参都为int型的函数。

#include <functional>
#include <iostream>
int f(int a, int b)
{
  return a+b;
}
int main()
{
	std::function<int(int, int)>func = f;
	cout<<f(1, 2)<<endl;      // 3
	system("pause");
	return 0;
}

我们再来先来看看以前C和C++98的用法:

#include<iostream>
using namespace std;

// c type global function
int c_func(int a, int b)
{
    return a + b;
}

//function object
struct functor
{
public:
    int operator() (int a, int b)
    {
        return a + b;
    }
};
int main()
{
   typedef int (*Func)(int ,int);
   Func func = c_func;
   cout<< func(1,2)<<endl;  //3
   
   functor ft;
   cout<<ft(1,2)<<endl;    //3
   return 0;
}

从上面我们可以看出,使用C++11的function类调用函数方便多了。

虽然是function是类模板,但其只有成员函数,无数据成员。
3. function的成员函数

成员函数声明说明
constructor构造函数:constructs a new std::function instance
destructor析构函数: destroys a std::function instance
operator=给定义的function对象赋值
operator bool检查定义的function对象是否包含一个有效的对象
operator()调用一个对象

4. std::function用法详解

  1. 调用普通函数
    #include <functional>
    #include <iostream>
     int f(int a, int b)
     {
       return a+b;
     }
     int main()
     {
     	std::function<int(int, int)>func = f;
     	cout<<func(1, 2)<<endl;      // 3
     	system("pause");
     	return 0;
     }
  1. 调用函数对象
#include<iostream>
using namespace std;

//function object
struct functor
{
public:
    int operator() (int a, int b)
    {
        return a + b;
    }
};
int main()
{
   functor ft;
   function<int(int,int)> func = ft();
   cout<<func(1,2)<<endl;    //3
   return 0;
}
  1. 调用模板函数对象
#include<iostream>
using namespace std;

//function object
template<class T>
struct functor
{
public:
    T operator() (T a, T b)
    {
        return a + b;
    }
};
int main()
{
   functor ft;
   function<int(int,int)> func = ft<int>();
   cout<<func(1,2)<<endl;    //3
   return 0;
}
  1. 调用lambda表达式
#include <functional>
#include <iostream>
using namespace std;
int main()
{
	auto f = [](const int a, const int b) {return a + b; };
	std::function<int(int, int)>func = f;
	cout << func(1, 2) << endl;      // 3
	system("pause");
	return 0;
}
  1. 调用类静态成员函数
#include <iostream>
#include <functional>
using namespace std;

class Plus
{
public:
    static int plus(int a, int b)
    {
        return a + b;
    }
};

int main()
{
    function<int(int, int)> f = &Plus::plus;
    cout << f(1, 2) << endl;     //3
    system("pause");                                       
    return 0;
}

6.调用类成员函数

#include <iostream>
#include <functional>
using namespace std;

class Plus
{
public:
    int plus(int a, int b)
    {
        return a + b;
    }
};

int main()
{
  Plus p;
    function<int(Plus&,int, int)> f = &Plus::plus;
  //function<int(const Plus,int, int)> f = &Plus::plus;
    cout << f(p,1, 2) << endl;     //3
    system("pause");                                       
    return 0;
}

7.调用类公有数据成员

#include <iostream>
#include <functional>
using namespace std;

class Plus
{
    Plus(int num_):num(num_){}
	public:
	    int plus(int a, int b)
	    {
	        return a + b;
	    }
	   int  num;
};

int main()
{
    const Plus p(2);
    function<int(const Plus&)> f = &Plus::num;
  //function<int(const Plus)> f = &Plus::num;
    cout << f(p) << endl;     //2
    system("pause");                                       
    return 0;
}

8.通过bind函数调用类成员函数

#include <iostream>
#include <functional>
using namespace std;

class Plus
{
public:
    int plus(int a, int b)
    {
        return a + b;
    }
};

int main()
{
   Plus p;
   // 指针形式调用成员函数
   function<int(int, int)> f = bind(&Plus::plus, &p, placeholders::_1, placeholders::_2);// placeholders::_1是占位符
   // 对象形式调用成员函数
   function<int(int, int)> f1 = bind(&Plus::plus, p, placeholders::_1, placeholders::_2);// placeholders::_1是占位符
   cout << f(1, 2) << endl;     //3
   cout << f1(1, 2) << endl;     //3
   system("pause");                                       
   return 0;
}

最后附上一段代码:

#include <iostream>
#include <map>
#include <functional>
using namespace std;
 
// 普通函数
int add(int i, int j) { return i + j; }
// lambda表达式
auto mod = [](int i, int j){return i % j; };
// 函数对象类
struct divide
{
	int operator() (int denominator, int divisor)
	{
		return denominator / divisor;
	}
};
 
///SubMain//
int main()
{
	map<char, function<int(int, int)>> binops = 
	{
		{ '+', add },
		{ '-', [](int i, int j){return i - j; } },
		{ '/', divide() }
	};
	cout << binops['+'](10, 5) << endl;
	cout << binops['-'](10, 5) << endl;
	cout << binops['/'](10, 5) << endl;
	system("pause");
	return 0;
}

参考:

  • 44
    点赞
  • 223
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
C++中的模板类是一种通用的类模板,可以定义具有通用行为的类,用于处理各种不同类型的数据。下面是使用C++模板类的基本方法: 1. 定义模板类: 使用关键字`template`和模板参数列表来定义一个模板类。模板参数列表可以包含类型参数和非类型参数。 ```cpp template <typename T> class TemplateClass { // 类的成员和方法定义 }; ``` 2. 实例化模板类: 在使用模板类之前,需要根据实际需要对其进行实例化来创建特定类型的对象。 ```cpp TemplateClass<int> obj1; // 实例化一个存储int类型数据的对象 TemplateClass<double> obj2; // 实例化一个存储double类型数据的对象 ``` 3. 使用模板类的成员和方法: 可以像使用普通类一样使用模板类的成员和方法。 ```cpp obj1.memberVariable = 10; // 设置成员变量的值 int value = obj1.memberFunction(); // 调用成员函数并获取返回值 ``` 4. 特化模板类(可选): 如果需要针对特定类型提供不同的实现,可以使用模板类的特化来为特定类型定义特定的行为。 ```cpp template <> class TemplateClass<char> { // 特定类型char的特化实现 }; ``` 注意事项: - 模板类的定义通常放在头文件中,以便在需要的地方进行包含。 - 模板类的成员函数的实现通常也需要放在头文件中,以避免链接错误。 - 模板类可以有多个模板参数,可以使用模板元编程技术进行更高级的操作。 - 在使用模板类时,编译器会根据实例化的类型生成对应的代码。每个类型的实例化都会生成不同的类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值