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的返回值是一个函数对象. 它的源文件太长了. 看不 ...
随机推荐
- FileUtils类介绍
Java的文件操作太基础,缺乏很多实用工具,比如对目录的操作,支持就非常的差了.如果你经常用Java操作文件或文件夹,你会觉得反复编写这些代码是令人沮丧的问题,而且要大量用到递归. 下面是的一个解决方 ...
- Mysql中查看表的类型InnoDB
问题描述: MySQL 数据表主要支持六种类型 ,分别是:BDB.HEAP.ISAM.MERGE.MYISAM.InnoBDB. 这六种又分为两类,一类是“事务安全型”(transaction-s ...
- modelsim使用命令
1. 常用仿真命令 vlib work // 建立work仿真库 vmap work wrok // 映射库 vlog -cover bcest *.v // 加覆盖率分析的编 ...
- Python 库大全
作者:Lingfeng Ai链接:http://www.zhihu.com/question/24590883/answer/92420471来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...
- SpringMVC项目接入Springfox实战遇到的问题集合
为了方便的管理项目中API接口,目前总是会写好接口后,然后又要去维护一个文档,这对于开发者来说太心累了, 在网上找了好多关于API接口管理和生成文档的资料,一次偶然跟51的大神交流发现了Swagger ...
- iOS OC开发代码规范
1.变量.类名.函数名 使用驼峰命名法 2.尽量使用完整的单词命名,尽量不采用 缩写单词 3.类名使用大写字母打头,前缀统一加上HH 例如:HHHomePageController 4.类的成员变量使 ...
- Can't connect to MySQL server on localhost (10061)解决方法
出现这种错误的原因是由于MySQL的服务被关闭的原因,重新启动一下服务就可以了,启动服务的操作如下: 右键[计算机]-[管理]
- android 59 LinearLayout 线性布局
##常见的布局* LinearLayout 线性布局线性布局往左右拉是拉不动的,> 线性布局的朝向 vertical|horizontal> 线性布局的权重 weight 和 0dip一起 ...
- LPC2378-Jlink 能下载程序,但是调试出现各种奇怪问题
LPC2378-Jlink调试经验 1.Jlink(d版的没有关系)不能下载程序? 把下载速率设置到500Khz及以下的速率. 2.Jlink能在500Khz的速率下载程序,但是不能调试?添加 ...
- FreeBSD更换默认csh为bash
1.安装bash cd /usr/ports/shells/bash make install 2.切换chsh(change shell) chsh -s /usr/local/bin/bash