gpt4 book ai didi

c++ - 有没有一种在集合上调用 lambda 函数的单行方法?

转载 作者:太空宇宙 更新时间:2023-11-04 15:12:33 24 4
gpt4 key购买 nike

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

我想知道我是否可以用这样的新类(class)来做到这一点,或者是否已经存在我不知道的类似内容。我对实现的样子有点困惑,尤其是对于像这样的链接函数调用。谢谢!

最佳答案

这是范围库的示例:

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

boost 范围

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);
}

Note that we don't store the expression template because Boost's facilities don't deal well with temporaries (Range v3 generates compilation errors on unsafe use).

打印

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++ - 有没有一种在集合上调用 lambda 函数的单行方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49930131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com