绑定Vs Lambda? [英] Bind Vs Lambda?

查看:110
本文介绍了绑定Vs Lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,优先考虑哪种风格:std :: bind Vs C ++ 0x。

I have a question about which style is preferred: std::bind Vs lambda in C++0x. I know that they serve -somehow- different purposes but lets take an example of intersecting functionality.

使用 lambda :我知道他们服务于某种不同的目的,

Using lambda:

uniform_int<> distribution(1, 6);
mt19937 engine;
// lambda style
auto dice = [&]() { return distribution(engine); };

使用 bind

uniform_int<> distribution(1, 6);
mt19937 engine;
// bind style
auto dice = bind(distribution, engine);

我们应该选择哪一个?为什么?假设与所提到的示例相比更复杂的情况。即一个对另一个的优点/缺点是什么?

Which one should we prefer? why? assuming more complex situations compared to the mentioned example. i.e. What are the advantages/disadvantages of one over the other?

推荐答案

正如你所说,bind和lambdas不完全

As you said, bind and lambdas don't quite exactly aim at the same goal.

例如,对于使用和编写STL算法,lambdas是明确的赢家,IMHO。

For instance, for using and composing STL algorithms, lambdas are clear winners, IMHO.

为了说明,我记得一个非常有趣的答案,在这里堆栈溢出,有人问了十六进制魔术数字的想法(如0xDEADBEEF,0xCAFEBABE,0xDEADDEAD等),并被告知,如果他是一个真正的C ++程序员,只需下载一个英语单词列表,并使用一个简单的C ++语言:

To illustrate, I remember a really funny answer, here on stack overflow, where someone asked for ideas of hex magic numbers, (like 0xDEADBEEF, 0xCAFEBABE, 0xDEADDEAD etc.) and was told that if he were a real C++ programmer he would simply have download a list of English words and use a simple one-liner of C++ :)

#include <iterator>
#include <string>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

int main()
{
    using namespace boost::lambda;
    std::ifstream ifs("wordsEn.txt");
    std::remove_copy_if(
        std::istream_iterator<std::string>(ifs),
        std::istream_iterator<std::string>(),
        std::ostream_iterator<std::string>(std::cout, "\n"),
        bind(&std::string::size, _1) != 8u
            ||
        bind(
            static_cast<std::string::size_type (std::string::*)(const char*, std::string::size_type) const>(
                &std::string::find_first_not_of
            ),
            _1,
            "abcdef",
            0u
        ) != std::string::npos
    );
}

这个片段在纯C ++ 98中打开英文单词文件,扫描每个字词,并以'a','b','c','d','e'或'f'字母只打印长度为8的字词。

This snippet, in pure C++98, open the English words file, scan each word and print only those of length 8 with 'a', 'b', 'c', 'd', 'e' or 'f' letters.

现在,打开C ++ 0X和lambda:

Now, turn on C++0X and lambda :

#include <iterator>
#include <string>
#include <algorithm>
#include <iostream>
#include <fstream>

int main()
{
 std::ifstream ifs("wordsEn.txt");
 std::copy_if(
    std::istream_iterator<std::string>(ifs),
    std::istream_iterator<std::string>(),
    std::ostream_iterator<std::string>(std::cout, "\n"),
    [](const std::string& s)
    {
       return (s.size() == 8 && 
               s.find_first_not_of("abcdef") == std::string::npos);
    }
 );
}

这还是有点重读(主要是因为istream_iterator业务) ,但比bind版本简单得多:)

This is still a bit heavy to read (mainly because of the istream_iterator business), but a lot simpler than the bind version :)

这篇关于绑定Vs Lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆