[英]How to encapsulate custom iterator in function using boost-range
Lately I was using boost-range
to create ranges over elements satisfying certain criteria. 最近我使用
boost-range
来创建满足某些标准的元素的范围。 In all cases I'm using the same kind of filtered range all the time, so that I tried to encapsulate this behaviour in an external function. 在所有情况下,我一直使用相同类型的过滤范围,因此我尝试将此行为封装在外部函数中。
This was the point where my problems started. 这是我的问题开始的地方。 Consider the following example.
请考虑以下示例。
#include <boost/range/adaptor/filtered.hpp>
#include <iostream>
#include <vector>
auto myFilter = [](const std::vector<int>& v, int r) {
return v | boost::adaptors::filtered([&r](auto v) { return v%r == 0; });
};
int main(int argc, const char* argv[])
{
using namespace boost::adaptors;
std::vector<int> input{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (auto& element : input | filtered([](auto v) {return v % 2 == 0; } ))
{
std::cout << "Element = " << element << std::endl;
}
std::cout << std::endl;
for (auto& element : myFilter(input,4))
{
std::cout << "Element = " << element << std::endl;
}
return 0;
}
The first for-loop behaves as expected printing 4 and 8. The second for-loop however prints just 4. Why is that? 第一个for-loop表现为预期的打印4和8.然后第二个for循环只打印4.为什么?
My second idea was to implement a class having a begin()
and end()
function. 我的第二个想法是实现一个具有
begin()
和end()
函数的类。 This should be a thin wrapper around a range object. 这应该是一个范围对象周围的薄包装器。
This was the solution, after fiddling out the type of the range iterator. 在摆弄范围迭代器的类型之后,这就是解决方案。
struct MyFilter {
MyFilter(const std::vector<int>& c, int r) : c(c), r(r), f([&r](auto v) { return v%r == 0; }) {
}
boost::range_detail::filtered_range<std::function<bool(int)>, std::vector<int>>::iterator begin() {
return rng.begin();
}
boost::range_detail::filtered_range<std::function<bool(int)>, std::vector<int>>::iterator end() {
return rng.end();
}
std::vector<int> c;
int r;
std::function<bool(int)> f;
boost::range_detail::filtered_range < std::function<bool(int)>, std::vector<int>> rng=c | boost::adaptors::filtered(f);
};
Usage should be something like: 用法应该是这样的:
for (auto& element : MyFilter(input, 4)) {
std::cout << "Element = " << element << std::endl;
}
Unfortunately, it prints again just the 4. Whichs is quite strange to me?? 不幸的是,它再次打印只是4.这对我来说很奇怪?
Now, I got the solution by myself. 现在,我自己得到了解决方案。 I have to remove the "&" in my lambda function to make it work!
我必须删除我的lambda函数中的“&”才能使它工作!
In: 在:
auto myFilter = [](const std::vector<int>& v, int r) {
return v | boost::adaptors::filtered([&r](auto v) { return v%r == 0; });
};
It returns another range adaptor while r
captured by reference becomes a dangling reference. 它返回另一个范围适配器,而通过引用捕获的
r
成为悬空引用。 To fix it capture r
by value: 要修复它,请按值捕获
r
:
auto myFilter = [](const std::vector<int>& v, int r) {
return v | boost::adaptors::filtered([r](auto v) { return v%r == 0; });
}; ^
+--- capture by value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.