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

直接代码:

 

代码段1:

 1 #include <iostream>
 2 #include <string>
 3 #include <boost/bind/bind.hpp>
 4 
 5 class some_class 
 6 {
 7 public:  
 8     typedef void result_type;  
 9     void print_string(const std::string& s) const 
10     {    std::cout << s << '\n';  }
11 };
12 void print_string(const std::string s) 
13 {  
14     std::cout << s << '\n';
15 }
16 int main() 
17 {  
18     boost::bind(&print_string,_1)("Hello func!");  //ok
19     some_class sc;  
20     boost::bind(&some_class::print_string,_1,_2)(sc,"Hello member!"); //ok
21     boost::bind(&some_class::print_string,_1,_2)(some_class(),"Hello member!"); //ok
22     boost::bind(&some_class::print_string,some_class(),"Hello member!")(); //ok
23     boost::bind(&some_class::print_string,some_class(),"Hello member!"); //warning
24     std::cout << std::endl;
25 }

 

代码段2:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <iterator>
 6 #include <list>
 7 
 8 #include <boost/bind/bind.hpp>
 9 
10 class personal_info 
11 {  
12     friend std::ostream& operator<< (  std::ostream& os,const personal_info& pi);
13 
14     std::string name_;  
15     std::string surname_;  
16     unsigned int age_;
17 public:  
18     personal_info(    const std::string& n,    const std::string& s,    unsigned int age):name_(n),surname_(s),age_(age) {}  
19     std::string name() const {    return name_;  }  
20     std::string surname() const {    return surname_;  }  
21     unsigned int age() const {    return age_;  }
22 };
23 std::ostream& operator<<(  std::ostream& os,const personal_info& pi) 
24 {  
25     os << pi.name() << ' ' <<    pi.surname() << ' ' << pi.age() << '\n';  
26     return os;
27 }
28 class personal_info_age_less_than :  public std::binary_function<  personal_info,personal_info,bool> 
29 {
30 public:  
31     bool operator()(  const personal_info& p1,const personal_info& p2) 
32     {    
33         return p1.age()<p2.age();  
34     }
35 };
36 
37 void main()
38 {
39     std::vector<personal_info> vec;
40     vec.push_back(personal_info("Little","John",30));
41     vec.push_back(personal_info("Friar", "Tuck",50));
42     vec.push_back(personal_info("Robin", "Hood",40));
43     std::sort( vec.begin(),  vec.end(),  
44         boost::bind( std::less<unsigned int>(),    
45             boost::bind(&personal_info::age,_1),    
46             boost::bind(&personal_info::age,_2)));
47 
48     std::sort(vec.begin(),vec.end(),personal_info_age_less_than());
49 
50     std::vector<int> ints;
51     ints.push_back(7);
52     ints.push_back(4);
53     ints.push_back(12);
54     ints.push_back(10);
55     //if (i>5 && i<=10) {  // Do something}
56     int count=std::count_if( ints.begin(),  
57         ints.end(),  
58         boost::bind( std::logical_and<bool>(),    
59             boost::bind(std::greater<int>(),_1,5),    
60             boost::bind(std::less_equal<int>(),_1,10)));
61     std::cout << count << '\n';
62     std::vector<int>::iterator int_it=std::find_if( ints.begin(),  
63         ints.end(),  
64         boost::bind( std::logical_and<bool>(),    
65             boost::bind(std::greater<int>(),_1,5),    
66             boost::bind(std::less_equal<int>(),_1,10)));
67     if (int_it!=ints.end()) 
68     {  
69         std::cout << *int_it << '\n';
70     }
71 
72     std::list<double> values;
73     values.push_back(10.0);
74     values.push_back(100.0);
75     values.push_back(1000.0);
76     //以下语句全部等价
77     std::transform( values.begin(), values.end(), values.begin(), boost::bind( std::multiplies<double>(),0.90, boost::bind<double>( std::multiplies<double>(),_1,1.10)));
78     std::transform( values.begin(), values.end(), values.begin(), boost::bind( std::multiplies<double>(),0.90, boost::bind( std::multiplies<double>(),_1,1.10)));
79     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)));
80     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));
81     
82     std::copy( values.begin(),  
83         values.end(),  
84         std::ostream_iterator<double>(std::cout,"\n"));
85 
86 }

 

代码段3:

 1 #include <iostream>
 2 #include <string>
 3 
 4 #include <boost/bind/bind.hpp>
 5 
 6 class tracer 
 7 {
 8 public:  
 9     tracer() {    std::cout << "tracer::tracer()\n";  }  
10     tracer(const tracer& other) {    std::cout << "tracer::tracer(const tracer& other)\n";  }  
11     tracer& operator=(const tracer& other) {    std::cout <<      "tracer& tracer::operator=(const tracer& other)\n";    return *this;  }  
12     ~tracer() {    std::cout << "tracer::~tracer()\n";  }  
13     void print(const std::string& s) const {    std::cout << s << '\n';  }
14 };
15 
16 void main()
17 {
18     tracer t;
19     //boost::bind(&tracer::print, t, _1)(std::string("I'm called on a copy of t\n"));
20     //boost::bind(&tracer::print, &t, _1)(std::string("I'm called on a copy of t\n"));
21     //boost::bind(&tracer::print, boost::ref(t), _1)(std::string("I'm called on a copy of t\n"));
22     //boost::bind(&tracer::print, boost::cref(t), _1)(std::string("I'm called on a copy of t\n"));
23     boost::bind(&tracer::print, _1, _2)(&t, std::string("I'm called on a copy of t\n"));
24 }

 

代码段4:

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 #include <algorithm>
 5 #include <iterator>
 6 
 7 #include <boost/bind/bind.hpp>
 8 
 9 void print_string(const std::string& s) {  std::cout << s << '\n';}
10 void print_string2(const std::map<int,std::string>::value_type& s) {  std::cout << s.second << '\n';}
11 void print_string3(const int& i) {  std::cout << i << '\n';}
12 
13 void main()
14 {
15     std::map<int,std::string> my_map;
16     my_map[0]="Boost";
17     my_map[1]="Bind";
18 
19     std::for_each( my_map.begin(),  
20         my_map.end(),  
21         boost::bind(&print_string, 
22         boost::bind( &std::map<int,std::string>::value_type::second,_1)));
23     std::for_each( my_map.begin(),  
24         my_map.end(),  
25         boost::bind(&print_string2, _1));
26     std::for_each( my_map.begin(),  
27         my_map.end(),  
28         boost::bind(&print_string3, 
29         boost::bind( &std::map<int,std::string>::value_type::first,_1)));
30     //std::for_each( my_map.begin(),  
31     //    my_map.end(),  
32     //    boost::bind(&print_string, _1)(&std::map<int,std::string>::value_type::second)); //error
33 
34 }

 

代码段5:

 1 #include <iostream>
 2 #include <string>
 3 #include <map>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 #include <boost/bind/bind.hpp>
 8 
 9 class print_size 
10 {  
11     typedef std::map<std::string,std::vector<int> > map_type;
12 public:  
13     typedef void result_type;  
14     result_type operator()(std::ostream& os, const map_type::value_type& x) const 
15     {    
16         os << x.second.size() << '\n';  
17     }
18     result_type operator()(std::ostream& os, const map_type& x) const //no use
19     {    
20         map_type::const_iterator iter = x.begin();
21         os << iter->second.size() << '\n';  
22     }
23 };
24 
25 int main() 
26 {  
27     typedef std::map<std::string,std::vector<int> > map_type; 
28     map_type m;  
29     m["Strange?"].push_back(1);  
30     m["Strange?"].push_back(2); 
31     m["Strange?"].push_back(3);
32     m["Weird?"].push_back(4); 
33     m["Weird?"].push_back(5); 
34     std::for_each(m.begin(),m.end(), boost::bind(print_size(),boost::ref(std::cout),_1));
35 }

 

代码段6:

 

 1 /*其它练习:boost::bind返回值*/
 2 
 3 #include <iostream>
 4 #include <string>
 5 #include <map>
 6 #include <vector>
 7 #include <algorithm>
 8 
 9 #include <boost/bind/bind.hpp>
10 
11 int TestAdd(int a, int b)
12 {
13     std::cout << "function inside" << std::endl;
14     return a+b;
15 }
16 
17 void main()
18 {
19     int ret = boost::bind(&TestAdd, _1, _2)(4, 6);
20     std::cout << "function outside ret = " << ret << std::endl;
21 }

 

 

 

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

posted @ 2014-05-07 20:13  仙人球球  Views(267)  Comments(0Edit  收藏  举报