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);
}

 

posted @ 2021-01-05 21:11  htj10  阅读(71)  评论(0编辑  收藏  举报
TOP