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编译运行。
boost::bind实践2——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》的更多相关文章
- 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 ...
- boost::bind实践
第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- boost::function和boost:bind取代虚函数
以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...
- 1,Boost -> Bind
#include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...
- boost::bind 和 boost::function 基本用法
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...
- 使用BOOST BIND库提高C++程序性能
Boost.Bind为函数和函数对象,值语义和指针提供语义了一致的语法.我们首先通过一些简单的例子来看看它的基本用法,之后我们会延伸到嵌套绑定以实现功能组合.理解bind用法的一个关键是理解占位符(p ...
- boost::bind 不能处理函数重载 (error: no matching function for call to 'bind')
前言 最近任务多.工期紧,没有时间更新博客,就水一期吧.虽然是水,也不能太失水准,刚好最近工作中遇到一个 boost::bind 的问题,花费了半天时间来定位解决,就说说它吧. 问题背景 项目中使用了 ...
- boost bind使用指南
bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不 ...
随机推荐
- Linux Mono OpenShift Cloud9 rhc
OpenShift Mono Asp.net MVC4 MySql 1..Net4.0+Mvc4,不要使用太新的.Net4.5.1+Mvc5. 2.MySql数据库:MySql.Data.Dll 从官 ...
- nopCommerce_3.00-Nop.Core.Caching
namespace Nop.Core.Caching { /// <summary> /// Cache manager interface /// </summary> pu ...
- Bzoj 3343: 教主的魔法 分块,二分
3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 821 Solved: 364[Submit][Status][Discuss ...
- MTK Android Driver知识大全
一.Display 1.lcm 相关概念1.1) MIPI接口:一共有三种接口:DBI(也做CPU或MCU接口).DPI(也叫RGB接口).DSI.在使用DSI接口时,目前75/77都只支持到2条da ...
- SRM 447(1-250pt, 1-500pt)
DIV1 250 水题...略. DIV1 500 抽象题意:有一个图,指定其中亮点p1和p2,删掉途中其他的一些点,使得p1和p2最短路大于3. 解法:使得p1和p2最短路为2的点一定要删掉.然后, ...
- Java--创建线程及常用方法
继承java.lang.Thread类--Thread类代表线程类 它的常用方法如下: static Thread currentThread():返回当前正在运行的线程对象的引用. static ...
- 机器学习笔记1——Linear Regression with One Variable
Linear Regression with One Variable Model Representation Recall that in *regression problems*, we ar ...
- 【转】【阮一峰的网络日志】Git 使用规范流程
作者: 阮一峰 日期: 2015年8月 5日 团队开发中,遵循一个合理.清晰的Git使用流程,是非常重要的. 否则,每个人都提交一堆杂乱无章的commit,项目很快就会变得难以协调和维护. 下面是Th ...
- PAT 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- BA - 读书雷达10本必读书
https://www.douban.com/doulist/43172796/ 用户故事与敏捷方法 入门篇之一: “是每个ThoughtWorks BA都读的经典入门书籍,详细介绍了用户故事及实用操 ...