任何使用boost MPL或类似功能进行迭代器过滤的方法

Any way of doing this iterator filter with boost MPL or similar

本文关键字:迭代器 过滤 方法 功能 boost MPL 任何使      更新时间:2023-10-16

本质上,我有几种情况使用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 ) );

或者诸如此类的傻事。