#include #include #include #include #include template class Remove_And_Increase { public: Remove_And_Increase(T const& target) : target{target} { } bool valid(T const& value) const { return value != target; } T transform(T const& value) const { return value + 1; } private: T target; }; template class Negative_To_Positive { public: Negative_To_Positive() { } bool valid(T const& value) { return value < 0; } T transform(T const& value) const { return -value; } }; template class Map_Filter { using value_type = typename Container::value_type; using iterator = typename Container::iterator; public: Map_Filter(Container& container, Policy policy = {}) : begin{container.begin()}, end{container.end()}, policy{policy} { to_next(); } value_type next() { if (done()) { throw std::out_of_range{"Reached the end"}; } iterator save { begin }; ++begin; to_next(); return policy.transform(*save); } bool done() const { return begin == end; } private: void to_next() { while (begin != end && !policy.valid(*begin)) { ++begin; } } iterator begin; iterator end; Policy policy; }; int main() { std::vector v { 1, 5, -2, 67, 1, 23, 15, -76, 1 }; std::list l { -1.2, 3.4, -5.6, -7.8, 9.0 }; { Map_Filter, Remove_And_Increase> mf { v, {1} }; while (!mf.done()) { std::cout << mf.next() << " "; } std::cout << std::endl; } { Map_Filter, Remove_And_Increase> mf { v, {0} }; while (!mf.done()) { std::cout << mf.next() << " "; } std::cout << std::endl; } { Map_Filter, Negative_To_Positive> mf { l, {} }; while (!mf.done()) { std::cout << mf.next() << " "; } std::cout << std::endl; } }