boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

直接代码:

代码段1:

 #include <iostream>
#include <string>
#include <boost/bind/bind.hpp> class some_class
{
public:
typedef void result_type;
void print_string(const std::string& s) const
{ std::cout << s << '\n'; }
};
void print_string(const std::string s)
{
std::cout << s << '\n';
}
int main()
{
boost::bind(&print_string,_1)("Hello func!"); //ok
some_class sc;
boost::bind(&some_class::print_string,_1,_2)(sc,"Hello member!"); //ok
boost::bind(&some_class::print_string,_1,_2)(some_class(),"Hello member!"); //ok
boost::bind(&some_class::print_string,some_class(),"Hello member!")(); //ok
boost::bind(&some_class::print_string,some_class(),"Hello member!"); //warning
std::cout << std::endl;
}

代码段2:

 #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <list> #include <boost/bind/bind.hpp> class personal_info
{
friend std::ostream& operator<< ( std::ostream& os,const personal_info& pi); std::string name_;
std::string surname_;
unsigned int age_;
public:
personal_info( const std::string& n, const std::string& s, unsigned int age):name_(n),surname_(s),age_(age) {}
std::string name() const { return name_; }
std::string surname() const { return surname_; }
unsigned int age() const { return age_; }
};
std::ostream& operator<<( std::ostream& os,const personal_info& pi)
{
os << pi.name() << ' ' << pi.surname() << ' ' << pi.age() << '\n';
return os;
}
class personal_info_age_less_than : public std::binary_function< personal_info,personal_info,bool>
{
public:
bool operator()( const personal_info& p1,const personal_info& p2)
{
return p1.age()<p2.age();
}
}; void main()
{
std::vector<personal_info> vec;
vec.push_back(personal_info("Little","John",));
vec.push_back(personal_info("Friar", "Tuck",));
vec.push_back(personal_info("Robin", "Hood",));
std::sort( vec.begin(), vec.end(),
boost::bind( std::less<unsigned int>(),
boost::bind(&personal_info::age,_1),
boost::bind(&personal_info::age,_2))); std::sort(vec.begin(),vec.end(),personal_info_age_less_than()); std::vector<int> ints;
ints.push_back();
ints.push_back();
ints.push_back();
ints.push_back();
//if (i>5 && i<=10) { // Do something}
int count=std::count_if( ints.begin(),
ints.end(),
boost::bind( std::logical_and<bool>(),
boost::bind(std::greater<int>(),_1,),
boost::bind(std::less_equal<int>(),_1,)));
std::cout << count << '\n';
std::vector<int>::iterator int_it=std::find_if( ints.begin(),
ints.end(),
boost::bind( std::logical_and<bool>(),
boost::bind(std::greater<int>(),_1,),
boost::bind(std::less_equal<int>(),_1,)));
if (int_it!=ints.end())
{
std::cout << *int_it << '\n';
} std::list<double> values;
values.push_back(10.0);
values.push_back(100.0);
values.push_back(1000.0);
//以下语句全部等价
std::transform( values.begin(), values.end(), values.begin(), boost::bind( std::multiplies<double>(),0.90, boost::bind<double>( std::multiplies<double>(),_1,1.10)));
std::transform( values.begin(), values.end(), values.begin(), boost::bind( std::multiplies<double>(),0.90, boost::bind( std::multiplies<double>(),_1,1.10)));
std::transform( values.begin(), values.end(), values.begin(), boost::bind( std::multiplies<double>(),0.90, boost::bind( boost::type<double>(), std::multiplies<double>(),_1,1.10)));
std::transform( values.begin(), values.end(), values.begin(), boost::bind<double>( std::multiplies<double>(), boost::bind<double>( std::multiplies<double>(),_1,1.10),0.90)); std::copy( values.begin(),
values.end(),
std::ostream_iterator<double>(std::cout,"\n")); }

代码段3:

 #include <iostream>
#include <string> #include <boost/bind/bind.hpp> class tracer
{
public:
tracer() { std::cout << "tracer::tracer()\n"; }
tracer(const tracer& other) { std::cout << "tracer::tracer(const tracer& other)\n"; }
tracer& operator=(const tracer& other) { std::cout << "tracer& tracer::operator=(const tracer& other)\n"; return *this; }
~tracer() { std::cout << "tracer::~tracer()\n"; }
void print(const std::string& s) const { std::cout << s << '\n'; }
}; void main()
{
tracer t;
//boost::bind(&tracer::print, t, _1)(std::string("I'm called on a copy of t\n"));
//boost::bind(&tracer::print, &t, _1)(std::string("I'm called on a copy of t\n"));
//boost::bind(&tracer::print, boost::ref(t), _1)(std::string("I'm called on a copy of t\n"));
//boost::bind(&tracer::print, boost::cref(t), _1)(std::string("I'm called on a copy of t\n"));
boost::bind(&tracer::print, _1, _2)(&t, std::string("I'm called on a copy of t\n"));
}

代码段4:

 #include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <iterator> #include <boost/bind/bind.hpp> void print_string(const std::string& s) { std::cout << s << '\n';}
void print_string2(const std::map<int,std::string>::value_type& s) { std::cout << s.second << '\n';}
void print_string3(const int& i) { std::cout << i << '\n';} void main()
{
std::map<int,std::string> my_map;
my_map[]="Boost";
my_map[]="Bind"; std::for_each( my_map.begin(),
my_map.end(),
boost::bind(&print_string,
boost::bind( &std::map<int,std::string>::value_type::second,_1)));
std::for_each( my_map.begin(),
my_map.end(),
boost::bind(&print_string2, _1));
std::for_each( my_map.begin(),
my_map.end(),
boost::bind(&print_string3,
boost::bind( &std::map<int,std::string>::value_type::first,_1)));
//std::for_each( my_map.begin(),
// my_map.end(),
// boost::bind(&print_string, _1)(&std::map<int,std::string>::value_type::second)); //error }

代码段5:

 #include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm> #include <boost/bind/bind.hpp> class print_size
{
typedef std::map<std::string,std::vector<int> > map_type;
public:
typedef void result_type;
result_type operator()(std::ostream& os, const map_type::value_type& x) const
{
os << x.second.size() << '\n';
}
result_type operator()(std::ostream& os, const map_type& x) const //no use
{
map_type::const_iterator iter = x.begin();
os << iter->second.size() << '\n';
}
}; int main()
{
typedef std::map<std::string,std::vector<int> > map_type;
map_type m;
m["Strange?"].push_back();
m["Strange?"].push_back();
m["Strange?"].push_back();
m["Weird?"].push_back();
m["Weird?"].push_back();
std::for_each(m.begin(),m.end(), boost::bind(print_size(),boost::ref(std::cout),_1));
}

代码段6:

 /*其它练习:boost::bind返回值*/

 #include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm> #include <boost/bind/bind.hpp> int TestAdd(int a, int b)
{
std::cout << "function inside" << std::endl;
return a+b;
} void main()
{
int ret = boost::bind(&TestAdd, _1, _2)(, );
std::cout << "function outside ret = " << ret << std::endl;
}

以上代码段全部通过VS2010 update1编译运行。

上一篇:Phonegap解决错误:Error initializing Cordova:Class not found


下一篇:#Cocos2d+lua#android+Eclipse工程编译设置