2.Boost之bind



1.Boost:bind

#include
<iostream>

#include
<boost/bind.hpp>

 

using
namespace
std;

using
namespace
boost;

 

int
f(int
a,
int
b = 12)

{

   
return
a +
b;

}

 

int
g(int
a,
int
b,
int
c)

{

   
return
a +
b +
c;

}

 

int
main(int
argc,
char *argv[])

{

 

   
//通过下面方法调用等价于f(1,2);

   
cout <<
"bind(f, 1, 2)() = " <<bind(f,
1, 2)() << endl;

   
//同样能够绑定部分参数,例如:下方表示第二个参数传递的是15

   
cout <<
"bind(f, 12, _1)(15) = " <<bind(f,
12, _1)(15) <<
endl;

   
//用两个占位符

   
cout <<
"bind(f, _1, _2)(1, 3) = " <<bind(f,
_1,
_2)(1, 3) <<
endl;

 

   
cout <<
"----------" <<
endl;

 

   
//引入参数调用的方式

   
int
i = 5;

   
cout <<
"bind(f, i, _1)(1) = " <<
bind(f,
i,
_1)(1) <<
endl;

 

   
cout <<
"----------" <<
endl;

 

   
//等价于g(1,2,3)

   
cout <<
"bind(g, 1, 2, 3)() = " <<bind(g,
1, 2, 3)()<< endl;

   
//用三个占位符的情况

   
cout <<
"bind(g,_1,_2,_3)(2,3,4) = " <<
bind(g,_1,_2,_3)(2,3,4)
<< endl;

 

 

   
cin.get();

   
return 0;

}

运行结果:

2.boost案例1

#include
<iostream>

#include
<string>

#include
<boost/bind.hpp>

#include
<vector>

#include
<algorithm>

#include
<functional>

 

using
namespace
std;

using
namespace
boost;

 

//绑定函数的默认值,继承二进制函数类的所有内容

class
add :public
std::binary_function<int,
int,
void>

{

public:

   
void operator()(int
i,
int
j)
const

   
{

       
std::cout
<< i +
j <<
std::endl;

   
}

};

 

void
add(int
i,
int
j)

{

   
std::cout
<< i +
j <<
std::endl;

}

 

void
main()

{

   
vector<int>
myv;

   
myv.push_back(11);

   
myv.push_back(23);

   
myv.push_back(34);

 

   
for_each(myv.begin(),
myv.end(),
bind(add,
13, _1));

   
cin.get();

}

运行结果:

3bind并不仅仅限于方法,下面的例子是绑定结构体的情况。

#include
<iostream>

#include
<boost/bind.hpp>

#include
<algorithm>

#include
<cassert>

 

using
namespace
std;

using
namespace
boost;

 

struct
F

{

   
int operator()(int
a,
int
b){
return
a -
b; };

   
bool operator()(long
a,
long
b){
return
a ==
b; };

};

 

struct
F2

{

   
int
s;

   
typedef
void
result_type;

   
void operator()(int
x)

   
{

       
s +=
x;

       
std::cout
<< "x = " <<
x <<"
s = "<<
s <<
std::endl;

   
}

};

 

int
main(int
argc,char
*argv[])

{

   
F
f;

   
int
x = 104;

 

   
//通过:bind<R>(f, ...)
这种语法,但是

   
cout <<
"bind<int>(f, _1, _2)(10,5) = " <<bind<int>(f,
_1,
_2)(10,5) <<
endl;

 

   
cout <<
"---------" <<
endl;

   
//通过:boost::bind(boost::type<R>(),f,_1,_2)(x,y);的方式进行绑定

   
cout <<
"boost::bind(boost::type<int>(), f, _1, 3)(8)= "<<boost::bind(boost::type<int>(),
f,
_1, 3)(8) <<
endl;

 

   
F2
f2 = { 0 };

   
int
a[] = { 1, 2, 3 };

 

   
cout <<
"---------" <<
endl;

   
//for_each是#include <algorithm>中的

   
for_each(a,
a + 3,
bind(std::ref(f2),
_1));

   
//下面的是#include <cassert>头文件中的

   
//assert(f2.s == 6);

 

   
cin.get();

   
return 0;

}

运行结果:

 

 

 

时间: 2024-09-20 06:05:04

2.Boost之bind的相关文章

基于boost asio开发的http server了解

给应用增加一个http远程访问接口,用于查询应用当前的状态.这也是为什么会要搞这个东西的原因之一.另外还有个原因,就是要通过标准的http协议,方便应用的分布式部署. 这个http server,完全是参照asio的example搞的,地址: http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/examples.html#boost_asio.examples.http_server_3.惟一的改动,就是在request_handle

GNU Radio协议数据包传递方式之一——消息机制

Message Passing Introduction GNU Radio was originally a streaming system with no other mechanism to pass data between blocks. Streams of data are a model that work well for samples, bits, etc., but are not really the right mechanism for control data,

boost::bind的使用方法

bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板. 用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不下去. 这里只记下它的用法: 9.1 对于普通函数 假如有函数 fun() 如下:  void fun(int x, int y) {   cout << x << ", " << y << endl;  } 现在我们看看怎么用 bind

关于boost::bind中fstream对象禁止拷贝的解决方法

fstream的拷贝构造函数是私有的,禁止fstream对象的拷贝. 比如,下面的程序编译出错,提示拷贝构造函数私有: #include<fstream> #include<iostream> #include<boost/thread/thread.hpp> using namespace std; void fun(ofstream &out) { std::cout<<"succeed!"<<endl; } in

boost库学习随记六:使用同步定时器、异步定时器、bind、成员函数回调处理、多线程的同步处理示例等

一.使用同步定时器 这个示例程序通过展示如何在一个定时器执行一个阻塞等待.       [cpp] view plaincopy   //makefile   #----------------------------------------------------------   #makefile helloworld测试用例   #   #   #   #   #-----------------------------------------------------------   gg

boost::bind 与 boost::function 的使用方法例子

啥也不说,直接看代码: #include <boost/bind.hpp> #include <boost/function.hpp> #include <iostream> using namespace boost; using namespace std; int f(int a, int b) { return a + b; } int g(int a, int b, int c) { return a + b * c; } class Point { publ

boost::bind替换为lambda

问题描述 boost::bind替换为lambda convenient_json jobj; typedef boost::signal<void(convenient_json)> signal_json_type; map<SHOW_FLAG,boost::shared_ptr<signal_json_type>>::iterator it = m_reg_global_module.find(flag); boost::bind(&signal_json

boost::bind 学习

 最近学习了太多与MacOS与Iphone相关的东西,因为不会有太多人有兴趣,学习的平台又是MacOS,不太喜欢MacOS下的输入法,所以写下来的东西少了很多.    等我学习的东西慢慢的与平台无关的时候,可能可以写下来的东西又会慢慢多起来吧.....不过我想早一批订阅我博客的人应该已经不会再看了,因为已经很少会有程序语言或者C++之类的信息,相关的东西应该都是关于计算机图形,游戏的.或者,早一批订阅我博客的人现在也已经毕业,工作了呢?    对了,这次的主题是boost:bind.在历经了bo

c++11-请帮忙看下async_accept的这种调用方法,没有使用Bind,看不懂...

问题描述 请帮忙看下async_accept的这种调用方法,没有使用Bind,看不懂... void AsyncAcceptManaged(ManagerAcceptHandler mgrHandler) { _acceptor.async_accept(_socket, [this, mgrHandler](boost::system::error_code error) { if (!error) { try { _socket.non_blocking(true); mgrHandler(