是否有一种单行方式可以在集合上调用lambda函数?

时间:2018-04-19 20:57:39

标签: c++ boost groovy stl

在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). // ...

我想知道我是否可以使用这样的新课程,或者我是否已经知道这样的事情。我有点难以理解实现的样子,特别是对于像这样的链接函数调用。谢谢!

2 个答案:

答案 0 :(得分:6)

这是范围库的一个示例:

范围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; 

答案 1 :(得分:1)

使用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;
}