【问题标题】:Is there a one-line way to call a lambda function on a collection?是否有一种在集合上调用 lambda 函数的单行方式?
【发布时间】:2018-09-30 11:47:56
【问题描述】:

在 Groovy 中,我可以像这样使用集合:

["a", "b"].each { println(it) }
[1, 2].collect { it * 2 }.grep { it % 3}.each { println(it) }

是否有 C++11/14 的等效语法?

Collection({"a", "b"}).Each([](auto i) { printf("%s\n", i); });
Collection({1, 2}).
  Collect([](auto i) { return i * 2; }).
  Grep([](auto i) { return i % 3; }).
  Each([](auto i) { printf("%d\n", i); };
Range(0, maxIdx). // ...

我想知道我是否可以使用这样的新课程来做到这一点,或者是否已经有类似的东西我不知道。我对实现的样子感到有些困惑,尤其是对于像这样的链接函数调用。谢谢!

【问题讨论】:

  • 这让我想起了range-v3
  • c++ 标准库具有执行这些步骤的功能,但不是以链接形式。但是,没有什么可以阻止现有的这种语法
  • for(auto& i: {"a", "b"}}) { /* do stuff to i here */ }
  • 我从没想过我会看到有人在 C++ 的 lambda 函数中使用 printf。去图吧。

标签: c++ boost groovy stl


【解决方案1】:

以下是范围库的示例:

范围 V3

Live On Wandbox

#include <range/v3/all.hpp>
#include <iostream>
#include <cstdio>
using namespace std::string_literals;
using namespace ranges::v3;
using std::vector;

int main() {
    auto print = [](auto const& v) { std::cout << v << "\n"; };

    for(auto sz : { "a", "b" }) print(sz);
    for_each({"a", "b"}, print);

    vector v { 1, 2, 3, 4, 5 };

    auto chain = 
           view::transform([](auto i) { return i*2; })
         | view::filter([](auto i) -> bool { return i % 3; });

    for_each(v | chain, print);

    auto constexpr Range = view::iota;
    for_each(Range(12, 24) | chain, print);
}

打印

a
b
a
b
2
4
8
10
26
28
32
34
38
40
44
46

提升范围

Live On Wandbox

#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <iostream>
using namespace boost::adaptors;
using std::vector;

int main() {
    auto print = [](auto const& v) { std::cout << v << "\n"; };

    for(auto sz : { "a", "b" }) print(sz);
    boost::for_each(vector {"a", "b"}, print);

    vector v { 1, 2, 3, 4, 5 };

    boost::for_each(v
            | transformed([](auto i) { return i*2; })
            | filtered([](auto i) -> bool { return i % 3; }),
         print);

    for_each(boost::irange(12, 24) 
            | transformed([](auto i) { return i*2; })
            | filtered([](auto i) -> bool { return i % 3; }),
        print);
}

请注意,我们不存储表达式模板,因为 Boost 的 设施不能很好地处理临时性(Range v3 生成 不安全使用的编译错误)。

打印

a
b
a
b
2
4
8
10
26
28
32
34
38
40
44
46

为了纯粹的乐趣

您可以使用纯 C++03 中的各种 Boost 库来做同样的事情:

Live On Coliru

#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/array.hpp>
#include <boost/foreach.hpp>
#include <boost/assign.hpp>
#include <boost/phoenix.hpp>
#include <iostream>

using namespace boost::adaptors;
using namespace boost::phoenix::arg_names;
using std::vector;

int main() {
    char const* ab[] = { "a", "b" };
    // who needs c++11 for ranged-for?
    BOOST_FOREACH(char const* sz, ab) std::cout << sz << "\n";

    // who needs c++11 for lambdas?
    boost::for_each(ab, std::cout << arg1 << "\n");

    // who needs c++11 for initializer lists?
    vector<int> v;
    using boost::assign::operator+=; // very dubious magic, but hey, we're having fun
    v += 1, 2, 3, 4, 5;

    // etc.
    boost::for_each(v | transformed(arg1 * 2) | filtered(arg1 % 3), std::cout << arg1 << "; ");
    std::cout << '\n';

    boost::for_each(boost::irange(12, 24) | transformed(arg1 * 2) | filtered(arg1 % 3), std::cout << arg1 << "; ");
    std::cout << '\n';
}

打印

a
b
a
b
2; 4; 8; 10; 
26; 28; 32; 34; 38; 40; 44; 46; 

【讨论】:

  • 增加了提升范围。也在这里C++11C++03
  • 很棒的答案,谢谢!我不太明白为什么答案有 6 个赞成票,但问题有 0 个。
【解决方案2】:

使用range-v3,会是这样的:

for (auto e : v
              | ranges::view::transform([](int n){ return 2 * n; })
              | ranges::view::filter([](int n){ return i % 3; }) )
{
    std::cout << e << std::endl;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    相关资源
    最近更新 更多