C++新特性探究(十四):function

相关博文:C++头文件<functional>和bind、placeholders占位符使用简单例子
相关博文:《Essential C++》笔记之设计一个泛型算法(二)
相关博文:C++Function Object Adapter之not1
相关博文:C++之再探参数绑定bind、bind1st、bind2nd、placeholders占位符
相关博文:C++新特性探究(十五):bind

  类模版 std::function 是可调用对象的包装器,可以包装除了类成员函数之外的所有可调用对象。包括,普通函数,函数指针,lambda,仿函数。
  通过指定的模板参数,它可以用统一的方式保存,并延迟执行它们。所谓的延迟执行,就是回调了。
在这里插入图片描述
(有人说function甚至可以取代多态)

例1:回顾——多态统一接口

在这里插入图片描述
附例1代码:

//小问学编程
#include <iostream>
#include <functional>
using namespace std;

class Oper
{
    
    
public:
	virtual int op(int,int) = 0;
};

class OperAdd:public Oper
{
    
    
public:
	int op(int i,int j)
	{
    
    
		return i+j;
	}
};
class OperMinus:public Oper
{
    
    
public:
	int op(int i,int j)
	{
    
    
		return i-j;
	}
};

int main()
{
    
    
    OperAdd add;
    cout<<add.op(4,5)<<endl;
    OperMinus minus;
    cout<<minus.op(4,5)<<endl;
	Oper *oper = new OperAdd;
	cout<<oper->op(4,5)<<endl;
	oper = new OperMinus;
	cout<<oper->op(10,4)<<endl;
	return 0;
}

例2:function统一接口

在这里插入图片描述
附例2代码:

//小问学编程
#include <iostream>
#include <functional>
#include <map>
using namespace std;

int add(int x,int y)
{
    
    
    return x+y;
}
int minux(int x,int y)
{
    
    
    return x-y;
}
typedef int(*MINUS)(int x,int y);

class Divide
{
    
    
public:
    int operator()(int x,int y)
    {
    
    
        return x/y;
    }
};

int main()
{
    
    
    auto op = function<int(int,int)>(add);
    cout<<op(1,4)<<endl;

    MINUS mi=minux;
    op=function<int(int,int)>(mi);
    cout<<op(4,5)<<endl;

    auto ou=function<int(int,int)>(minux);
    cout<<ou(4,5)<<endl;

    auto mul=[](int x,int y){
    
    return x*y;};

    op=function<int(int,int)>(mul);
    cout<<op(4,5)<<endl;

    Divide d;
    op=function<int(int,int)>(d);
    cout<<op(10,5)<<endl;

    cout<<"========================"<<endl;
    map<string,function<int(int,int)>>mp={
    
    
    {
    
    "+",add},
    {
    
    "-",mi},
    {
    
    "*",mul},
    {
    
    "/",d},
    };

    cout<<mp["+"](1,2)<<endl;
    cout<<mp["-"](10,2)<<endl;
    cout<<mp["*"](2,3)<<endl;
    cout<<mp["/"](10,2)<<endl;
    return 0;
}

例3:function作参数类型实现回调

在这里插入图片描述
附例3代码:

//小问学编程
#include <iostream>
#include<algorithm>
#include<functional>
using namespace std;

void selectSort(int* p,int n,function<int(int,int)>compare)
{
    
    
	for(int i=0;i<n-1;i++)
	{
    
    
		for(int j=i+1;j<n;j++)
		{
    
    
			if(compare(p[i],p[j]))
			{
    
    
				p[i]^=p[j];
				p[j]^=p[i];
				p[i]^=p[j];
			}
		}
	}
}

int main()
{
    
    
	int arr[]={
    
    1,3,5,7,9,2,4,6,8,10};
	selectSort(arr,sizeof(arr)/sizeof(*arr),[](int x,int y){
    
    return x>y;});

	for_each(arr,arr+10,[](int& i){
    
    cout<<i<<endl;});
	return 0;
}

例4:function作类成员实现回调

在这里插入图片描述
附例4代码:

//小问学编程
#include <iostream>
#include <functional>
using namespace std;

class functor
{
    
    
public:
    void operator()()
    {
    
    
        cout<<"_FUNCTION_"<<endl;
    }
};

class A
{
    
    
public:
    A(const function<void()> & cb)
        :_callback(cb)
    {
    
    }
    void notify()
    {
    
    
        _callback();
    }

    function<void()> _callback;
};

int main(int argc, char *argv[])
{
    
    
    functor fct; //事先定义好的函数
    A a(fct);
    a.notify();

    return 0;
}

例5:观察者模式

在这里插入图片描述
附例5代码:

//小问学编程
#include <iostream>
#include<functional>
#include<list>
using namespace std;

class FunctorA
{
    
    
public:
	void operator()()
	{
    
    cout<<"class Functor A"<<endl;}
};
class FunctorB
{
    
    
public:
	void operator()()
	{
    
    cout<<"class Functor B"<<endl;}
};
class FunctorC
{
    
    
public:
	void operator()()
	{
    
    cout<<"class Functor C"<<endl;}
};

class Object
{
    
    
public:
	Object(FunctorA a,FunctorB b,FunctorC c)
	{
    
    
		_list.push_back(a);
		_list.push_back(b);
		_list.push_back(c);
	}
	void notify()
	{
    
    
	    for(auto& item:_list)
        {
    
    
            item();
        }
	}
private:
	list<function<void(void)>> _list;
};

int main()
{
    
    
	FunctorA a;
	FunctorB b;
	FunctorC c;
	Object obj(a,b,c);
	obj.notify();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43297891/article/details/113754128
今日推荐