[英]Is it possible to use boost::filter_iterator for output?
I am using std::transform
with an std::back_inserter
to append elements to an std::deque
. 我使用
std::transform
和std::back_inserter
将元素追加到std::deque
。 Now the transformation may fail and will return a invalid object (say an uninitialized boost::optional
or a null pointer) in some cases. 现在转换可能会失败,并且在某些情况下会返回一个无效对象(比如一个未初始化的
boost::optional
或一个空指针)。 I would like to filter out the invalid objects from getting appended. 我想过滤掉附加的无效对象。
I thought about using boost::filter_iterator
, but not sure how to present the end()
parameter of the filtered range. 我想过使用
boost::filter_iterator
,但不知道如何呈现过滤范围的end()
参数。
The documentation of boost::filter_iterator
suggests that output filtering is possible. boost::filter_iterator
的文档表明输出过滤是可能的。 Should I just specialize operator ==
for std::back_insert_iterator
in this case to always return false? 在这种情况下,我应该专门为
operator ==
for std::back_insert_iterator
返回false吗?
In addition to this, if I want to append values of initialized boost::optional
or pointers, can I chain boost::filter_iterator
and boost::indirect_iterator
? 除此之外,如果我想追加初始化的
boost::optional
或指针的值,我可以链接boost::filter_iterator
和boost::indirect_iterator
吗?
I am trying to avoid rolling out my own transform_valid
function that takes an optional extractor
function. 我试图避免推出我自己的
transform_valid
函数,该函数采用可选的extractor
函数。
Is it even possible to use filter_iterator
as an output iterator? 甚至可以使用
filter_iterator
作为输出迭代器吗?
I suggest using boost range (algorithms & adaptors) for ease of use, you'd write: 我建议使用提升范围(算法和适配器)以方便使用,你写道:
boost::copy(
data | transformed(makeT) | filtered(validate) /* | indirected */,
std::back_inserter(queue));
Here is a complete working example of that: 这是一个完整的工作示例:
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/optional.hpp>
#include <vector>
#include <deque>
typedef boost::optional<int> T;
typedef std::deque<T> Q;
static T makeT(int i)
{
if (i%2) return T();
else return i;
}
static bool validate(const T& optional)
{
return (bool) optional; // select the optional that had a value set
}
int main()
{
static const int data[] = { 1,2,3,4,5,6,7,8,9 };
Q q;
using boost::adaptors::filtered;
using boost::adaptors::transformed;
// note how Boost Range elegantly supports an int[] as an input range
boost::copy(data | transformed(makeT) | filtered(validate), std::back_inserter(q));
// demo output: 2, 4, 6, 8 printed
for (Q::const_iterator it=q.begin(); it!=q.end(); ++it)
{
std::cout << (*it? "set" : "unset") << "\t" << it->get_value_or(0) << std::endl;
}
return 0;
}
Update 更新
With a little help from this answer: Use boost::optional together with boost::adaptors::indirected 在这个答案的帮助下: 使用boost :: optional和boost :: adapters :: indirected
I now include an elegant demonstration of using the indirected
range adaptor as well for immediate output of the queue (dereferencing the optionals): 我现在包括一个优雅的演示,使用
indirected
范围适配器以及队列的立即输出(取消引用选项):
Note that for (smart) pointer types there would obviously be no need to provide the
pointee<>
specialisation.请注意,对于(智能)指针类型, 显然不需要提供
pointee<>
。 I reckon this is by design:optional<> is not, and does not model, a pointer
我认为这是设计的:
optional<> is not, and does not model, a pointer
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/optional.hpp>
namespace boost {
template<typename P> struct pointee<optional<P> > {
typedef typename optional<P>::value_type type;
};
}
typedef boost::optional<int> T;
static T makeT(int i) { return i%2? T() : i; }
static bool validate(const T& optional) { return (bool) optional; }
int main() {
using namespace boost::adaptors;
static int data[] = { 1,2,3,4,5,6,7,8,9 };
boost::copy(data | transformed(makeT)
| filtered(validate)
| indirected,
std::ostream_iterator<int>(std::cout, ", "));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.