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 }

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

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

时间: 2024-10-10 16:02:45

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

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

代码段1: 1 #include <boost/function.hpp> 2 #include <iostream> 3 4 5 float mul_ints(int x, int y) { return ((float)x) * y; } 6 struct int_div { 7 float operator()(int x, int y) const { return ((float)x)/y; }; 8 }; 9 10 int main() 11 { 12 boost::f

boost::bind实践

第一部分源码为基础实践: 1 /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ 2 /*bind的用法*/ 3 4 #include <iostream> 5 #include <algorithm> 6 #include <functional> 7 #include <vector> 8 9 #include <boost/bind/bind.hpp&g

以boost::function和boost:bind取代虚函数

转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function和boost::bind,大多数情况下,你都不用上贼船. boost::function和boost::bind已经纳入了std::tr1,这或许是C++0x最值得期待的功能,它将彻底改变C++库的设计方式,以及应用程序的编写方式. Scott

Boost::bind学习

1什么是函数对象 2Boostbind 绑定自由函数 绑定全部参数 不绑定全部参数 绑定类的函数 boostbind绑定的值是拷贝形式的值传递 1.什么是函数对象 在了解函数对象之前,应该先知道什么是函数指针,函数指针不再介绍了.函数对象用起来比函数指针更加灵活,且可以实现内联函数. 函数对象是具有函数功能的对象.怎么才能使对象具有函数的功能呢?答案是重载操作符().加入要实现两个整数相加: class Add { public: int operator()( int a, int b) {

Boost::Bind 基础

先了解一下:函数对象 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.[1] 一个类对象,表现出一个函数的特征,就是通过"对象名+(参数列表)"的方式使用一个 类对象,如果没有上下文,完全可以把它看作一个函数对待.这是通过重载类的 operator()来实现的.比如,对于调用 int s = sum(1, 1); 你可能把它看作一个函数调用: int sum(int i, int j) { return i+j; } 但很可能

boost::bind

bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind. bind接受的第一个参数必须是一个可调用对象f,包括函数,函数指针,函数对象和成员函数,之后bind接受最多9个参数,参数的数量必须与f的参数数量相等 _1,_2这些一直可以到9,是占位符,必须在绑定表达式中提供函数要求的所有参数,无论是真实参数还是占位符均可以.占位符不可以超过函数参数数量. 绑定普通函数: 1.#include<boost/bind

boost::bind 详解

使用 boost::bind是标准库函数std::bind1st和std::bind2nd的一种泛化形式.其可以支持函数对象.函数.函数指针.成员函数指针,并且绑定任意参数到某个指定值上或者将输入参数传入任意位置. 1. 通过functions和function pointers使用bind 给定如下函数: 1 int f(int a, int b) 2 { 3 return a + b; 4 } 5 6 int g(int a, int b, int c) 7 { 8 return a + b

boost::bind boost::function

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <boost/bind.hpp> #include <boost/function.hpp> #include <iostream&g

1,Boost -&gt; Bind

#include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> using namespace std; void dprint(int x,int y) { cout << x << " " <<y <<endl; } class Bind_test { public: void setData(int x,i