dro*_*del 0 c++ groovy boost stl
在Groovy中,我可以使用这样的集合:
["a", "b"].each { println(it) }
[1, 2].collect { it * 2 }.grep { it % 3}.each { println(it) }
Run Code Online (Sandbox Code Playgroud)
是否有与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). // ...
Run Code Online (Sandbox Code Playgroud)
我想知道我是否可以用这样的新课程来做这件事,或者我是否已经知道这样的事情.我对实现甚至看起来有点难以理解,特别是对于像这样的链接函数调用.谢谢!
以下是范围库的示例:
#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);
}
Run Code Online (Sandbox Code Playgroud)
打印
a
b
a
b
2
4
8
10
26
28
32
34
38
40
44
46
Run Code Online (Sandbox Code Playgroud)
#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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,我们不存储表达式模板,因为Boost的工具不能很好地处理临时工(Range v3会在不安全的使用中生成编译错误).
打印
a
b
a
b
2
4
8
10
26
28
32
34
38
40
44
46
Run Code Online (Sandbox Code Playgroud)
你可以在纯C++ 03中使用各种Boost库来做同样的事情:
#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';
}
Run Code Online (Sandbox Code Playgroud)
打印
a
b
a
b
2; 4; 8; 10;
26; 28; 32; 34; 38; 40; 44; 46;
Run Code Online (Sandbox Code Playgroud)