C++ Boost::bind函数包装器使用,boost::bind与伪函数的绑定使用
2017-04-25 22:43
429 查看
在我们使用stl的 一些算法的时候,比如find_if,for_each等,需要使用仿函数,如果仿函数有2个参数,但是算法需要一个一元的仿函数的时候,我们可以使用适配器,boost库中boost::bind就帮助我们定义了函数适配器
下面主要讲了boost::bind如何适配函数,成员函数,Lambada函数,伪函数
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <boost\bind.hpp>
#include <functional>
using namespace std;
int add(int a,int b){
cout << a + b << endl;
return a+b;
}
struct Module{
private:
int num;
public:
Module(int x):num(x){
}
void test(int a ,int b){
cout << "testmodule" << a + b << endl;
}
void testA(int a ,int b){
cout << "testmodule" << a + b + num << endl;
}
};
struct Student:public binary_function<int,int,void>{
void operator()(int a, int b){
cout << a + b << endl;
}
};
void main(){
//boost::bind简单的将就是函数适配器 两个参数的函数通过bind可以变为1个参数使用
//bind可以绑定函数 lambada 伪函数
vector<int> myvec;
myvec.push_back(10);
myvec.push_back(20);
myvec.push_back(30);
for_each(myvec.begin(),myvec.end(),boost::bind(add,20,_1));
//bind绑定lambada 这种情况是ERROR
//for_each(myvec.begin(),myvec.end(), boost::bind([](int a,int b){cout << a + b << endl;},20,_1) );
//bind绑定lambada需要使用函数包装器
auto function = std::function<void(int,int)>([](int a,int b){cout << a + b << endl;});
for_each(myvec.begin(),myvec.end(),boost::bind(function,22,_1));
//boost::bind绑定成员函数
Module testModule(100);
for_each(myvec.begin(),myvec.end(),boost::bind(&Module::testA,&testModule,33,_1));
//boost::bind绑定伪函数 Student必须继承二进制函数类
for_each(myvec.begin(),myvec.end(),boost::bind(Student(),52,_1));
cin.get();
}
void main2(){
std::vector<int> myvec;
myvec.push_back(1);
myvec.push_back(2);
myvec.push_back(3);
//std::for_each(myvec.begin(),myvec.end(),[](int& data){});
std::for_each(myvec.begin(),myvec.end(),boost::bind(add,10,_1));
//函数包装器
function<void(int,int)> myfunc = [](int a,int b){cout << a + b << endl;};
std::for_each(myvec.begin(),myvec.end(),boost::bind(myfunc,20,_1));
cin.get();
}
主要是了解函数适配器的作用,以及bind各种函数的细节
下面主要讲了boost::bind如何适配函数,成员函数,Lambada函数,伪函数
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <boost\bind.hpp>
#include <functional>
using namespace std;
int add(int a,int b){
cout << a + b << endl;
return a+b;
}
struct Module{
private:
int num;
public:
Module(int x):num(x){
}
void test(int a ,int b){
cout << "testmodule" << a + b << endl;
}
void testA(int a ,int b){
cout << "testmodule" << a + b + num << endl;
}
};
struct Student:public binary_function<int,int,void>{
void operator()(int a, int b){
cout << a + b << endl;
}
};
void main(){
//boost::bind简单的将就是函数适配器 两个参数的函数通过bind可以变为1个参数使用
//bind可以绑定函数 lambada 伪函数
vector<int> myvec;
myvec.push_back(10);
myvec.push_back(20);
myvec.push_back(30);
for_each(myvec.begin(),myvec.end(),boost::bind(add,20,_1));
//bind绑定lambada 这种情况是ERROR
//for_each(myvec.begin(),myvec.end(), boost::bind([](int a,int b){cout << a + b << endl;},20,_1) );
//bind绑定lambada需要使用函数包装器
auto function = std::function<void(int,int)>([](int a,int b){cout << a + b << endl;});
for_each(myvec.begin(),myvec.end(),boost::bind(function,22,_1));
//boost::bind绑定成员函数
Module testModule(100);
for_each(myvec.begin(),myvec.end(),boost::bind(&Module::testA,&testModule,33,_1));
//boost::bind绑定伪函数 Student必须继承二进制函数类
for_each(myvec.begin(),myvec.end(),boost::bind(Student(),52,_1));
cin.get();
}
void main2(){
std::vector<int> myvec;
myvec.push_back(1);
myvec.push_back(2);
myvec.push_back(3);
//std::for_each(myvec.begin(),myvec.end(),[](int& data){});
std::for_each(myvec.begin(),myvec.end(),boost::bind(add,10,_1));
//函数包装器
function<void(int,int)> myfunc = [](int a,int b){cout << a + b << endl;};
std::for_each(myvec.begin(),myvec.end(),boost::bind(myfunc,20,_1));
cin.get();
}
主要是了解函数适配器的作用,以及bind各种函数的细节
相关文章推荐
- 使用boost.bind绑定class的成员函数作为boost thread的入口
- C++关于tr1中绑定成员函数的bind使用
- 【C++】boost::bind和函数对象一起使用实现便捷的异步编程
- jQuery使用bind函数实现绑定多个事件的方法
- 为什么使用boost::function绑定类成员函数时,类的析构函数不能为虚函数
- C++函数包装器与引用包装器,函数绑定器的使用
- 关于boost::function与boost::bind函数的使用心得
- [Boost基础]函数与回调——bind绑定
- javascript使用bind()函数绑定this
- boost::bind 绑定成员函数
- 使用Socket套接字绑定函数bind的一个细节
- boost::bind 函数绑定
- boost::bind无参非成员函数的绑定和调用(一)
- 关于boost::function与boost::bind函数的使用心得
- 关于boost::function与boost::bind函数的使用心得
- BOOST线程、锁与Bind函数的使用
- Boost关于bind的使用以及函数对象和传递参数的问题
- boost::bind绑定成员变量,函数书嵌套
- Mootools中使用bind给函数绑定对象
- boost::bind boost::function绑定万能函数测试2(类成员函数和虚函数)