boost::bind实践

时间:2023-03-08 18:00:20

第一部分源码为基础实践:

 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/
/*bind的用法*/ #include <iostream>
#include <algorithm>
#include <functional>
#include <vector> #include <boost/bind/bind.hpp>
#include <boost/bind/apply.hpp>
#include <boost/smart_ptr/shared_ptr.hpp> using namespace std;
using namespace boost; int f(int a, int b)
{
return a + b;
} int g(int a, int b, int c)
{
return a + b + c;
} bool preFind(int a, int b)
{
return a<b;
}
bool preFind2(int a, int b, int c)
{
return a<b && a<c;
}
struct predicate
{
typedef void result_type;
void operator() (int a, int b)
{
cout << a << "+" << b << "=" << a+b << endl;
}
};
struct X
{
void f(int a)
{
cout << "a = " << a << endl;
}
void f2(int a, int b)
{
cout << a << "+" << b << "=" << a+b << endl;
}
predicate pre;
int arrElem;
int *parrElem;
}; void main()
{
int arr[] = {, , , , };
//int ret1 = std::count_if(arr, arr+5, boost::bind(preFind, _1, 2));
//cout << ret1 << endl;
//int ret2 = std::count_if(arr, arr+5, boost::bind(preFind, 2, _2));
//cout << ret2 << endl;
//std::for_each (arr, arr+5, boost::bind(preFind, _1, 2));
//std::for_each (arr, arr+5, boost::bind(predicate(), _1, 2));
//std::for_each (arr, arr+5, boost::bind(boost::type<void>(), predicate(), _1, 2));
//std::for_each (arr, arr+5, boost::bind(preFind2, _1, 2, 3));
//std::for_each (arr, arr+5, boost::bind(preFind2, _1, 2, _1));
//boost::bind(preFind2, 5, _2, 3);
//std::for_each (arr, arr+5, boost::bind(preFind2, 5, _2, 3)); //boost::bind(preFind, _1, 2);
//boost::bind(preFind, _2, 2);
//boost::bind(preFind, _1, _2);
//boost::bind(preFind, _2, _1);
//boost::bind(preFind2, _1, _2, _3);
//boost::bind(predicate(), _1, _2);
//boost::bind(boost::type<void>(), predicate(), _1, _2); //X x;
//boost::bind(&X::f, boost::ref(x), _1);
//boost::bind(&X::f, _1, 1);
//boost::bind(&X::f, _1, _2);
//boost::bind(&X::pre, _1);
//std::for_each (arr, arr+5, boost::bind(&X::f, x, _1));
//std::for_each (arr, arr+5, boost::bind(&X::f2, boost::ref(x), _1, 2));
//std::for_each (arr, arr+5, boost::bind(&X::f2, &x, _1, 2));
//boost::shared_ptr<X> p(new X);
//std::for_each (arr, arr+5, boost::bind(&X::f2, p, _1, 2));
std::vector<X*> arrs;
for (int i= ; i!= ; ++i)
{
arrs.push_back(new X);
}
std::for_each (arrs.begin(), arrs.end(), boost::bind(&X::arrElem, _1));
std::for_each (arrs.begin(), arrs.end(), boost::bind(&X::f, _1, ));
}

第二部分源码为实际应用:

 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/
/*bind的用法*/ #include <iostream>
#include <algorithm>
#include <functional>
#include <vector> #include <boost/bind/bind.hpp>
#include <boost/smart_ptr/shared_ptr.hpp> using namespace std;
using namespace boost; class status
{
public:
std::string name_;
bool ok_;
public:
status(const std::string& name):name_(name),ok_(true) {}
void break_it() { ok_=false; }
bool is_broken() const { return ok_; }
void report() const
{
std::cout << name_.c_str() << " is " << (ok_ ? "working nominally":"terribly broken") << std::endl;
}
}; void nine_arguments( int i1,int i2,int i3,int i4, int i5,int i6,int i7,int i8, int i9)
{
std::cout << i1 << i2 << i3 << i4 << i5 << i6 << i7 << i8 << i9 << '\n';
}
int main()
{
int i1=,i2=,i3=,i4=,i5=,i6=,i7=,i8=,i9=;
(boost::bind(&nine_arguments,_9,_2,_1,_6,_3,_8,_4,_5,_7))(i1,i2,i3,i4,i5,i6,i7,i8,i9); //std::vector<status> statuses;
//statuses.push_back(status("status 1"));
//statuses.push_back(status("status 2"));
//statuses.push_back(status("status 3"));
//statuses.push_back(status("status 4"));
//statuses[1].break_it();
//statuses[2].break_it();
////method 1:
//for (std::vector<status>::iterator it=statuses.begin(); it!=statuses.end();++it)
//{
// it->report();
//}
////method 2: ok
//std::for_each( statuses.begin(), statuses.end(), std::mem_fun_ref(&status::report));
////method 3: ok
//std::for_each( statuses.begin(), statuses.end(), boost::bind(&status::report, _1));
////others ok
//std::for_each( statuses.begin(), statuses.end(), boost::bind(&status::name_, _1)); //std::vector<status*> p_statuses;
//p_statuses.push_back(new status("status 1"));
//p_statuses.push_back(new status("status 2"));
//p_statuses.push_back(new status("status 3"));
//p_statuses.push_back(new status("status 4"));
//p_statuses[1]->break_it();
//p_statuses[2]->break_it();
////method 2: ok
//std::for_each( p_statuses.begin(), p_statuses.end(), std::mem_fun(&status::report));
////method 3: ok
//std::for_each( p_statuses.begin(), p_statuses.end(), boost::bind(&status::report, _1)); std::vector<boost::shared_ptr<status> > s_statuses;
s_statuses.push_back( boost::shared_ptr<status>(new status("status 1")));
s_statuses.push_back( boost::shared_ptr<status>(new status("status 2")));
s_statuses.push_back( boost::shared_ptr<status>(new status("status 3")));
s_statuses.push_back( boost::shared_ptr<status>(new status("status 4")));
s_statuses[]->break_it();
s_statuses[]->break_it();
//method 2: error
//std::for_each( s_statuses.begin(), s_statuses.end(), std::mem_fun(&status::report));
//method 3: ok
std::for_each( s_statuses.begin(), s_statuses.end(), boost::bind(&status::report, _1)); }