方法一、仿函数
#include "stdafx.h"
#include <iostream>
using namespace std;
//仿函数,
class myFunctor
{
public:
myFunctor(int i):r(i){}//构造函数
//重载operator()
int operator() (int tmp)
{
return tmp + r;
}
private:
int r;
};
int main()
{
myFunctor obj(10);//调用构造函数
std::cout << obj(1) << std::endl;//调用仿函数
system("pause");
return 0;
}
编译输出
11
请按任意键继续. . .
方法二、std::bind绑定器
#include "stdafx.h"
#include <iostream>
#include <functional> //std::function
using namespace std;
using namespace placeholders;
//普通函数
void func()
{
cout << "func " << endl;
}
//类
class Test
{
public:
//静态函数
static int funcStatic(int tmp)
{
cout << "Test::funcStatic ";
return tmp;
}
//普通成员函数
void func(int x, int y)
{
cout << "Test::fun " << x << " " << y << endl;
}
int a;
};
//仿函数
class MyFunctor
{
public:
MyFunctor() {}//构造函数
//重载operator()
int operator() (int tmp)
{
cout << "myFunctor::operator ";
return tmp;
}
};
int main()
{
//1、绑定普通函数
function<void(void)> f1 = func;
f1();
//2、绑定类中的静态函数
function<int(int)> f2 = Test::funcStatic;
cout << f2(1) << endl;
//3、绑定类中的仿函数,绑定对象
MyFunctor obj;
function<int(int)> f3 = obj;
cout << f3(2) << endl;
//4、绑定类中成员函数
Test testObj;
function<void(int, int)> f4 = bind(&Test::func, &testObj, _1, _2);
f4(1, 2); //等价于,testObj.func(11,22)
//5、绑定成员变量
function<int &()> f5 = bind(&Test::a, &testObj);
f5() = 11;//等价于testObj.a = 11
cout << "Test.a=" << testObj.a << endl;
system("pause");
return 0;
}
编译输出
func
Test::funcStatic 1
myFunctor::operator 2
Test::fun 1 2
Test.a=11
请按任意键继续. . .
#include "stdafx.h"
#include <iostream>
#include <functional> //std::bind
using namespace std;
//普通函数
void func(int x,int y)
{
cout << "func "<<x << " "<<y << endl;
}
int main()
{
//下面三种形式都作用都是一样的
cout << "-----------------------------" << endl;
bind(func, 11, 22)();
cout << "-----------------------------" << endl;
//使用占位符
//std::placeholders::_1,函数调用时,被第一个参数替换
//std::placeholders::_2,函数调用时,被第二个参数替换
bind(func, std::placeholders::_1, std::placeholders::_2)(11, 22);
cout << "-----------------------------" << endl;
//引入命名空间
using namespace std::placeholders;
bind(func, _1, _2)(11, 22);
system("pause");
return 0;
}
编译输出
-----------------------------
func 11 22
-----------------------------
func 11 22
-----------------------------
func 11 22
请按任意键继续. . .
方法三 lambda表达式
用于定义并创建匿名的函数的对象,以简化编程工作。
语法形式
[函数对象参数] (操作符重载函数参数)mutable或exception声明 ->返回值类型{函数体}
[] (int x, int y) -> int { int z = x + y; return z; }
#include "stdafx.h"
#include <iostream>
#include <functional> //std::function
using namespace std;
using namespace placeholders;
int g_tmp = 1;
class MyClass
{
public:
MyClass();
~MyClass();
int i = 0;
void func()
{
auto f1 = [=]() {
cout << i << endl;
cout << g_tmp << endl;
};
auto f2 = [&]() {
cout << i << endl;
};
//只是捕获成员变量,全局变量
auto f3 = [this]() {
cout << i << endl;
cout << g_tmp << endl;
};
}
};
MyClass::MyClass()
{
}
MyClass::~MyClass()
{
}
int main()
{
int a = 0;
int b = 0;
auto f1 = []() {};
auto f2 = [a, b]() {cout << a << "," << b << endl; };
auto f3 = [a, b](int x, int y) {
cout << a << "," << b << endl;
cout << "x=" << x << endl;
cout << "y=" << y << endl;
};
f3(10, 20);
//以值传递方式传给lambda表达式
auto f4 = [=] {cout << a << "," << b << endl; };
//以引用方式捕获外部变量
auto f5 = [&] {cout << a << "," << b << endl; };
//a以值传递,其它以引用方式传递
auto f6 = [&,a]{cout << a << "," << b << endl; };
//a以引用传递,其它以值方式传递
auto f7 = [=, &a] {cout << a << "," << b << endl; };
//默认情况下,lambda函数,以const修饰函数体,值传递无法修改,想修改加mutable
//值传递是,lambda新建一个变量,外部变量给这个变量赋值,
auto f8 = [=]()mutable
{
a++;
{cout << a << "," << b << endl; };
};
f8();
cout << a << endl;
//引用传递
auto f9 = [&]()mutable
{
a++;
{cout << a << "," << b << endl; };
};
f9();
cout << a << endl;
cout << "..........................." << endl;
//lambda表达式有返回值
function<int(int)> func1= [](int a) { return a + 1; };
cout << "func1(10)=" << func1(10) << endl;
function<int(int)> func2 = bind([](int a) {return a + 1; }, placeholders::_1);
cout << "func2(10)=" << func2(10) << endl;
auto func3 = [](int x, int y) -> int { return x + y; };
//没有引用外部变量的lambda表达式可以转换为函数指针,反之任何形式的函数指针都不能转换为lambda表达式
//定义一个函数指针
typedef int(*PFUNC)(int, int);
PFUNC p1 = func3;//将func3转换为函数指针
cout <<"测试函数指针:"<< p1(10, 10) << endl;
cout << "..........................." << endl;
system("pause");
return 0;
}
编译输出:
0,0
x=10
y=20
1,0
0
1,0
1
...........................
func1(10)=11
func2(10)=11
测试函数指针:20
...........................
请按任意键继续. . .
番外:
lamda的优势
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//选择大于5的证书存入容器,并打印
vector<int> numList;
vector<int> largeNumList;
int tmp = 5;
void FunFind(int n)
{
if ( n > tmp )
{
largeNumList.push_back(n);
}
}
void FunPrint(int n)
{
cout << n << " ";
}
int main()
{
for (int i =0; i <=10; i++)
{
numList.push_back(i);
}
cout << "---------------------------------" << endl;
for (auto p1 = numList.begin();p1 != numList.end(); p1++)
{
if (*p1 > tmp)
{
largeNumList.push_back(*p1);
}
}
for (auto p1 = largeNumList.begin(); p1 !=largeNumList.end(); p1++)
{
cout << *p1 << " ";
}
cout << endl;
largeNumList.clear();
cout << "---------------------------------" << endl;
//传统回调函数的方式
for_each(numList.begin(), numList.end(), FunFind);
for_each(largeNumList.begin(), largeNumList.end(), FunPrint);
cout << endl;
largeNumList.clear();
cout << "---------------------------------" << endl;
//lambda表达式的方式
for_each(numList.begin(), numList.end(),
[](int n) {
if (n > tmp)
{
largeNumList.push_back(n);
}
});
for_each(largeNumList.begin(), largeNumList.end(),
[](int n) {
cout << n << " ";
});
cout << endl;
system("pause");
return 0;
}
|