C++中的标准模板库STL

注:主要基于C++11标准

std::function

例子:

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

int fun(int a, int b)
{
    return a + b;
}

class CCaller
{
public:
    int operator()(int a, int b)
    {
        std::cout << a << std::endl;
        cout << b << endl;
        return 0;
    }
};

void main()
{
    //std::function 可以表示函数、Lambda表达式、函数类对象
    std::function<int(int, int)> f;
    f = fun;
    int sum = f(3, 4);
    cout << f(3, 4) << endl;
    f = [](int a, int b)->int
        {
            return a*b;
        };
    int a = f(3, 4);
    cout << a << endl;
    CCaller caller;
    f = caller;
    f(3, 4);
}

常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。

昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。
原文地址:https://www.cnblogs.com/htj10/p/14237979.html