Get every nth element of the iterator range

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.

Edit
To 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.

Related questions:
C ++ / STL: std :: transform with given step? Non-operable step iterator with non-random access iterators

+7
source share
2 answers

Boost.Range provides a step adapter . Using boost::begin / boost::end will provide you with associated iterators.

+9
source

You can use boost::filter_iterator with a predicate like:

  template< typename T, int N > struct EveryNth { bool operator()(const T&) { return m_count++ % N == 0; } EveryNth() : m_count(0) {} private: int m_count; }; 
+5
source

All Articles