任何使用boost MPL或类似功能进行迭代器过滤的方法
Any way of doing this iterator filter with boost MPL or similar
本质上,我有几种情况使用boost::filter_iterator
来过滤迭代器中的某些条件。在这种情况下,我想同时过滤两个条件,我们已经有了一些预先存在的代码,但我想知道是否有一种惯用的方法可以通过boost或标准库来实现这一点:
/*! TODO: Surely there should be something in std/boost to achieve this??? */
/*! Filter for things that satisfy F1 and F2 */
template <
typename F1,
typename F2,
typename ArgT
>
struct filter_and
{
F1 f1;
F2 f2;
filter_and(F1 _f1, F2 _f2): f1(_f1), f2(_f2)
{}
inline bool operator() (ArgT const& arg) const
{
return f1(arg) && f2(arg);
}
};
如果一个解决方案需要c++11,那么只要最新的MSVC能够处理它,它就应该很好。
试试这个:make_filter_iterator( it, [=](value_type const& v) { return f1(v) && f2(v); } );
为了更花哨的东西。。。
bool and_in_order() { return true; }
template<typename F0, typename Funcs...>
bool and_in_order( F0&& f0, Funcs&&... funcs ) {
return f0() && and_in_order(funcs...);
}
template<typename... Funcs>
struct and_unary_functors {
std::tuple<Funcs...> funcs;
template<typename Arg, typename seq=typename make_seq<sizeof...(Funcs)>::type>
bool operator()(Arg&& arg) const;
template<typename Arg, int...s>
bool operator()<Arg, seq<s...>>(Arg&& arg) const {
return and_in_order( [&](){ return get<s>(funcs)(arg); }... );
}
};
template<typename... Funcs>
and_unary_functors<Funcs> make_and_unary( Funcs const&... funcs ) {
return {std::make_tuple(funcs...)};
};
auto filter_it = make_filter_iterator( base_iterator, make_and_unary( f1, f2, f3, f4 ) );
或者诸如此类的傻事。
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 为什么output_editor Concept不需要output_e迭代器标记
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 跟随整数索引列表的自定义类迭代器
- 不明白迭代器,引用和指针失效,一个例子
- 我可以使用反向迭代器作为ForwardIt吗
- ESP8266单片机矢量迭代器的C++问题
- 如何在C++中将迭代器作为函数参数传递
- 是否应避免从非常量迭代器转换为常量迭代器?
- 如何在 c++ 中将字符串迭代器变量传递给函数?
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 任何使用boost MPL或类似功能进行迭代器过滤的方法