/*
g++ bind.cc --std=c++11
bind 可绑定的内容
1.functions
2. function objects 即防函数
3. member functions 必须是某个对象的地址
4. data members 必须是某个对象的地址
*/
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;
class A
{
public:
void fun_3(int k,int m)
{
cout<<"print: k="<<k<<",m="<<m<<endl;
cout<<"print: a="<<a<<",b="<<b<<endl;
}
int multiply()
{
return a*b;
}
//private:
int a;
int b;
};
void fun_1(int x,int y,int z)
{
cout<<"print: x=" <<x<<",y="<< y << ",z=" <<z<<endl;
}
void fun_2(int &a,int &b)
{
a++;
b++;
cout<<"print: a=" <<a<<",b="<<b<<endl;
}
//有返回值的仿函数
class SUM//有返回值的仿函数
{
private:
long sum_D;
public:
SUM() :sum_D(0)
{
//cout<<"init sum_D="<<sum_D<<endl;
}
void operator()(int elem)
{
sum_D += elem;
}
operator double()
{
return static_cast<double>(sum_D);
}
};
template<class T>
class less1 //判断是否小
{
public:
less1() {}
T operator()(T &x1,T &x2)
{
if (x2 > x1)
{
cout << x2 <<" > " << x1 <<endl;
}
else
{
cout << x2 <<" < " << x1 <<endl;
}
return (x2 > x1)? 0:1;
}
};
int main(int argc, char * argv[])
{
//-----------------------------------------------绑定函数--------------------------------------------------------------
//f1的类型为 function<void(int, int, int)>
auto f1 = std::bind(fun_1,1,2,3); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3
f1(); //print: x=1,y=2,z=3
auto f2 = std::bind(fun_1, placeholders::_1,placeholders::_2,3);
//表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f2 的第一,二个参数指定
f2(1,2);//print: x=1,y=2,z=3
auto f3 = std::bind(fun_1,placeholders::_2,placeholders::_1,3);
//表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数指定
//注意: f2 和 f3 的区别。
f3(1,2);//print: x=2,y=1,z=3
int m = 2;
int n = 3;
auto f4 = std::bind(fun_2, placeholders::_1, n); //表示绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。
f4(m); //print: m=3,n=4
cout<<"m="<<m<<endl;//m=3 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的,如m
cout<<"n="<<n<<endl;//n=3 说明:bind对于预先绑定的函数参数是通过值传递的,如n
//-----------------------------------------------绑定成员函数--------------------------------------------------------------
A a {123,456};//C++ 11新支持的,这对a的成员函数进行初始化
//f5的类型为 function<void(int, int)>
auto f5 = std::bind(&A::fun_3, a,placeholders::_1,placeholders::_2); //使用auto关键字
f5(10,20);//调用a.fun_3(10,20),print: k=10,m=20
auto f6 = std::bind(&A::multiply, placeholders::_1); //绑定成员函数方式1
cout<<"f6() = "<<f6(a)<<endl;//输出 123*456=56088
auto f7 = std::bind(&A::multiply,a); //绑定成员函数方式2
cout<<"f7() = "<<f7()<<endl;//也是输出 123*456=56088
std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2);
fc(10,20); //调用a.fun_3(10,20) print: k=10,m=20
//---------------------------------------------绑定成员对象----------------------------------------------------------------
A a2 {100,200};//C++ 11新支持的,这对a2的成员函数进行初始化
a2.fun_3(0,1);
//绑定成员对象
{
auto c1 = std::bind(&A::a,a2);
cout << "c1="<<c1()<<endl;
auto c2 = std::bind(&A::b,a2);
cout << "c2="<<c2()<<endl;
}
//---------------------------------------------绑定防函数----------------------------------------------------------------
vector<int> vec{1,5,65,3,8,2,1,6,20,15,156,21,26,32,58};
//SUM(); //可以这样用
{
cout <<"sum="<<for_each(vec.begin(), vec.end(), SUM())<<endl;//输出sum=419
int a=11,b=22;
auto c1 = std::bind(less1<int>(),std::placeholders::_1,a);
c1(b);//输出11 < 22
for_each(vec.begin(), vec.end(), c1);
}
return 0;
}