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的返回值是一个函数对象. 它的源文件太长了. 看不 ...
随机推荐
- C# 4.0 新特性
http://www.cnblogs.com/webabcd/archive/2010/05/27/1744899.html 在MVC中Controller的action方法 常用的:可选参数和参数默 ...
- Android 快捷方式
1. 需要权限: <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT&quo ...
- Test2014-3-1 魅力值比较
魅力值比较 [问题描述] 大学生恋爱的问题造成了数量众多的异地恋,有许多J大的女生早早被Q大男生追走,这导致了J大男生的强烈不满.就在吐血高调地向一位J大美女展开攻势的之后,J大男生终于爆发了. 为了 ...
- Dijkstra算法求解最短路径分析
最短路径是图论算法中的经典问题.图分为有向图.无向图,路径权值有正值.负值,针对不同的情况需要分别选用不同的算法.在维基上面给出了各种不同的场景应用不同的算法的基本原则:最短路问题. 针对无向图,正权 ...
- 宁波Uber优步司机奖励政策(2月1日~2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【转载】怎么理解Condition
注:本文主要介绍了Condition和ReentrantLock工具实现独占锁的部分代码原理,Condition能在线程之间协调通信主要是AbstractQueuedSynchronizer和cond ...
- JAVA 面向对象-2-继承(Inheritance)
i.继承(Inheritance) 1.继承的概念 继承:在面向对象编程的过程中,通过扩展一个已有的类,并继承该类的属性和行为,来创建一个新的类. 继承是面向对象编程最重要的特征之一. 继承的优点:1 ...
- Android中使EditText失去焦点,edittext禁止弹出键盘[转]
转自http://www.cnblogs.com/yejiurui/archive/2013/01/02/2841945.html 在我们的应用中,有时候一进入一个页面, EditText默认就会自动 ...
- 站在巨人的肩膀上学习Android开发
我们知道,一開始最好的学习方法是模仿,尤其是模仿巨人. 那说到Android开发的模仿自然就是分析并研究主流程序的布局.实现方法.进而提升自己的技术. 第一招----逆向project 要分析&quo ...
- Codeforces 114A-Cifera(暴力)
A. Cifera time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...