直接代码:

代码段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编译运行。

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

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

    代码段1: #include <boost/function.hpp> #include <iostream> float mul_ints(int x, int y) { r ...

  2. boost::bind实践

    第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...

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

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  4. boost::function和boost:bind取代虚函数

    以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...

  5. 1,Boost -> Bind

    #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...

  6. boost::bind 和 boost::function 基本用法

    这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...

  7. 使用BOOST BIND库提高C++程序性能

    Boost.Bind为函数和函数对象,值语义和指针提供语义了一致的语法.我们首先通过一些简单的例子来看看它的基本用法,之后我们会延伸到嵌套绑定以实现功能组合.理解bind用法的一个关键是理解占位符(p ...

  8. boost::bind 不能处理函数重载 (error: no matching function for call to 'bind')

    前言 最近任务多.工期紧,没有时间更新博客,就水一期吧.虽然是水,也不能太失水准,刚好最近工作中遇到一个 boost::bind 的问题,花费了半天时间来定位解决,就说说它吧. 问题背景 项目中使用了 ...

  9. boost bind使用指南

    bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不 ...

随机推荐

  1. [GRYZ2014]迷宫问题

    设有一个N*N方格的迷宫,入口和出口分别在左上角和右上角,迷宫格子中分别放有0和1,0表示可走,1表示不能走,迷宫走的规则如图.当迷宫给出之后,找出一条从入口到出口的通路. 输入:N N*N的迷宫 输 ...

  2. maven构建带版本号和日期的war包名

    21166312 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !import ...

  3. 使用aespython进行ECB加解密示例

    CBC示例: from aespython import cbc_mode, key_expander, aes_cipher key = 'MluJMEZegVmrnWDaBiG1j7lqpkxNH ...

  4. Python按行读文件对比

    1. 最基本的读文件方法: # File: readline-example-1.py   file = open("sample.txt")   while 1:     lin ...

  5. 系统内存和CPU管理、监控

    本博文的主要内容有 .系统内存管理.监控:vmstat和free -mt .系统CPU管理.监控:sar -u.mpstat.uptime linux系统内存和CPU是在系统运行的过程中不断消耗的资源 ...

  6. 使用VNC实现多用户登录linux系统

    vmare版本:12.0.0 build-2985596

  7. boost库在工作(37)网络UDP服务端之七

    前面介绍的都是网络TCP的服务器和客户端,其实还有UDP的服务器和客户端,同时也有同步和异步之分.UDP与TCP最大的区别,就是TCP是基于连接的,而UDP是无连接的.这里所谓的连接是指对方中断服务时 ...

  8. [Ionic] Ionic Quickstart for Windows

    1. Install ionic 2. Create ionic app ionic start myApp tabs //create a app cd myApp ionic serve // o ...

  9. mysql触发器的作用及语法

    触发器是一种特殊的存储过程,它在插入,删除或改动特定表中的数据时触发运行,它比数据库本身标准的功能有更精细和更复杂的数据控制能力. 数据库触发器有下面的作用: 1.安全性.能够基于数据库的值使用户具有 ...

  10. oracle6

    转换函数  介绍 转换函数用于将数据类型从一种转为另外一种.在某些情况下,oracle server允许值的数据类型和实际的不一样,这时oracle server会隐含的转化数据类型 比如: crea ...