Is there a standard implementation (which means stdlib or boost) of an iterator that wraps another iterator and gives only every nth element?
At first I thought it was possible with a suitable predicate and boost :: filter_iterator, but the predicate only gets the value, not the base iterator, so it cannot determine the distance to the start.
EditTo give additional information: The iterator should be compatible with functions such as std::transform or std::copy . Therefore, it should be used as stdlib iterators.
std::transform
std::copy
Related questions:C ++ / STL: std :: transform with given step? Non-operable step iterator with non-random access iterators
Boost.Range provides a step adapter . Using boost::begin / boost::end will provide you with associated iterators.
boost::begin
boost::end
You can use boost::filter_iterator with a predicate like:
boost::filter_iterator
template< typename T, int N > struct EveryNth { bool operator()(const T&) { return m_count++ % N == 0; } EveryNth() : m_count(0) {} private: int m_count; };