`

boost bind

阅读更多
bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind.
bind接受的第一个参数必须是一个可调用对象f,包括函数,函数指针,函数对象和成员函数,之后bind接受最多9个参数,参数的数量必须与f的参数数量相等
_1,_2这些一直可以到9,是占位符,必须在绑定表达式中提供函数要求的所有参数,无论是真实参数还是占位符均可以。占位符不可以超过函数参数数量。
绑定普通函数:
#include<boost/bind.hpp>
#include<iostream>
using namespace std;
using namespace boost;

void fun(int a,int b){
        cout << a+b << endl;
}

int main()
{
        bind(fun,1,2)();//fun(1,2)
        bind(fun,_1,_2)(1,2);//fun(1,2)
        bind(fun,_2,_1)(1,2);//fun(2,1)
        bind(fun,_2,_2)(1,2);//fun(2,2)
        bind(fun,_1,3)(1);//fun(1,3)
}


3
3
3
4
4

绑定成员函数:
#include<boost/bind.hpp>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace boost;
using namespace std;

struct point
{
    int x,y;
    point(int a=0,int b=0):x(a),y(b){}
    void print(){
        cout << "(" << x << "," << y << ")\n";
    }
    void setX(int a){
        cout << "setX:" << a << endl;
    }
    void setXY(int x,int y){
        cout << "setX:" << x << ",setY:" << y << endl;
    }
    void setXYZ(int x,int y,int z){
        cout << "setX:" << x << ",setY:" << y << "setZ:" << z << endl;
    }
};

int main()
{
    point p1,p2;
    bind(&point::setX,p1,_1)(10);
    bind(&point::setXY,p1,_1,_2)(10,20);
    bind(&point::setXYZ,p2,_1,_2,_3)(10,20,30);
    vector<point> v(10);
    //for_each的时候只需要_1就可以了
    for_each(v.begin(),v.end(),bind(&point::print,_1));
    for_each(v.begin(),v.end(),bind(&point::setX,_1,10));
    for_each(v.begin(),v.end(),bind(&point::setXY,_1,10,20));
    for_each(v.begin(),v.end(),bind(&point::setXYZ,_1,10,20,30));
}

setX:10
setX:10,setY:20
setX:10,setY:20setZ:30
(0,0)
(0,0)
(0,0)
(0,0)
(0,0)
(0,0)
(0,0)
(0,0)
(0,0)
(0,0)
setX:10
setX:10
setX:10
setX:10
setX:10
setX:10
setX:10
setX:10
setX:10
setX:10
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30
setX:10,setY:20setZ:30

分享到:
评论

相关推荐

    c++ boost bind

    boost stl一些常用函数的使用训练

    基于boost的bind与function的消息处理框架

    算是一个消息处理框架吧,用于说函数对象function与bind的基本用法; 比较适合入门boost的function与bind的基本用法

    THE BOOST C++ LIBRARIES

    3.2 Boost.Bind 3.3 Boost.Ref 3.4 Boost.Function 3.5 Boost.Lambda 3.6 Exercises Chapter 4: Event Handling 4.1 General 4.2 Signals 4.3 Connections 4.4 Exercises Chapter 5: String Handling 5.1 General ...

    tcp server boost asio

    基于boost的asio封装的高性能TCP服务器。asio已经有很好的事件封装机制,只有底层...3、运用最新的C++11语法规范实现全部代码(lamda override bind) 原创博客地址: http://blog.csdn.net/wang19840301 欢迎留言讨论

    boost 1.41 中文文档,使用帮助,教程手册

    汉化 boost 文档,致力 boost 推广。 如果你对本项目有兴趣,欢迎加入,相关说明请查阅项目论坛: https://groups.google.com/ 到目前为止,各人贡献的译文如下: 贡献者 贡献的译文 alai04 accumulators, any, ...

    Boost带领你远远地超越了C++标准库,它使得C++编程更优雅、更有活力、更高产。首先,我们系统地介绍一下Boost库的主要组成和它们的主要用法

    Björn Karlsson为中级至高级的C++开发者描述了所有58...可在调用点进行定义的函数对象:Boost.Bind 和 Boost.Lambda 更灵活的回调机制:Boost.Function 可管理的信号和响应动作(又称为Observer模式):Boost.Signals

    BOOST入门笔记

    boost库基本使用方法,使用cmake2.6构建工程,包含nocopyable\singleton\asio\filesystem\bind\thread\futuer等

    Beyond.the.C++ - Standard.Library.An.Introduction.to.Boost

    practice solutions for performing type conversions and lexical conversions &lt;br...Boost.Bind and Boost.Lambda &lt;br&gt;More flexible callbacks with Boost.Function &lt;br&gt;Managed signals and slots (a.k.a....

    Beyond the C++ Standard Library An Introduction to Boost

    practice solutions for performing type conversions and lexical conversions &lt;br...Boost.Bind and Boost.Lambda &lt;br&gt;More flexible callbacks with Boost.Function &lt;br&gt;Managed signals and slots (a.k.a....

    boost库学习资料,可以与我交流

    本人看完此资料,受益非浅,拿出来大家一起分享,一起交流心得。

    Beyond.the.C.plus.plus.Standard.Library.An.Introduction.to.Boost

    在阅读本书的过程中,读者需要注意本书的排版风格,作者在写作过程中,大量使用了函数名和关键字,例如,“删除”使用了函数名“delete”,“绑定”使用了函数名“bind”,“类型定义”使用了关键字“typedef”等等...

    downloads.part1.rar

    atomic-boost-1.70.0.tar.gz boostorg-beast-boost-1.70.0.tar.gz boostorg-bimap-boost-1.70.0.tar.gz boostorg-bind-boost-1.70.0.tar.gz boostorg-build-boost-1.70.0.tar.gz boostorg-callable_traits-boost-...

    downloads.part3.rar

    atomic-boost-1.70.0.tar.gz boostorg-beast-boost-1.70.0.tar.gz boostorg-bimap-boost-1.70.0.tar.gz boostorg-bind-boost-1.70.0.tar.gz boostorg-build-boost-1.70.0.tar.gz boostorg-callable_traits-boost-...

    downloads.part2.rar

    atomic-boost-1.70.0.tar.gz boostorg-beast-boost-1.70.0.tar.gz boostorg-bimap-boost-1.70.0.tar.gz boostorg-bind-boost-1.70.0.tar.gz boostorg-build-boost-1.70.0.tar.gz boostorg-callable_traits-boost-...

    C++ function、bind以及lamda表达式

    本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制。之所以把这三块放在一起讲,是因为这三块之间有着非常密切的关系,通过对比学习,加深对这部分内容的理解。在开始之间...

    sockc:socks5中继服务器(作为csock的下游)

    boost(&gt; = 1.42,boost-system,boost-filesystem,boost-program_options,boost-asio,boost-bind,boost-pool) libcrypto ++ Qt(&gt; = 5.5,使用QtQuick2,QML) 和cmake(&gt; = 2.8) 安装 $ git clone sockc $ ...

    patrex:括号感知的标记化正则表达式

    boost::bind(&Foo::bar, boost::ref( *this ), ...) 如您所知, boost::bind可以使用成员函数的引用和指针,因此可以将所有此类调用简化为 boost::bind(&Foo::bar, this, ...) 我们不希望盲目搜索和替换boost::ref

    HelloWorld:Hello World 演示测试项目

    你好,世界Hello World 演示测试项目结构体 CB { typedef boost::variant&lt; boost&gt; , boost::function&lt;void&gt; &gt; ...&lt; str&gt; f( boost::bind( &CB::fun2, this, _1 ) );callback_["A"] = f ; boost::get&lt; boost&gt; &gt;(callbac

    pipeline:基于n3534的流水线实现

    auto grep_error = std::bind(grep, "Error.*", _1, _2); (boost::pipeline::from(input) | trim | grep_error | [] (const std::string& item) { return "-&gt; " + item; } | output ).run(pool); 反馈 尽管该库...

Global site tag (gtag.js) - Google Analytics