Skipping iterator

£可爱£侵袭症+ 提交于 2019-11-28 01:50:46

I think you want boost::adaptors::strided

struct TrueOnEven {
 template< typename T >
 bool operator()(const T&) { return mCount++ % 2 == 0; }
 TrueOnEven() : mCount(0) {}
 private:
  int mCount;
};

int main() {
 std::vector< int > tVec, tOtherVec;
 ...
 typedef boost::filter_iterator< TrueOnEven, int > TakeEvenFilterType;

 std::copy( 
  TakeEvenFilterType(tVec.begin(), tVec.end()),
  TakeEvenFilterType(tVec.end(), tVec.end()),
  std::back_inserter(tOtherVec));
}

To be honest, this is anything else than nice and intuitive. I wrote a simple "Enumerator" library including lazy integrated queries to avoid hotchpotch like the above.. It allows you to write:

Query::From(tVec.begin(), tVec.end())
.Skip<2>()
.ToStlSequence(std::back_inserter(tOtherVec));

where Skip<2> basically instantiates a generalized "Filter" which skips every N-th (in this case every second) element...

Cheers,

Paul

Here's Boost's filter iterator. It is exactly what you want.

UPDATE: Sorry, read wrongly-ish. Here's a list of all iterator funkiness in Boost:

http://www.boost.org/doc/libs/1_46_1/libs/iterator/doc/#specialized-adaptors

I think a plain iterator_adaptor with an overloaded operator++ that increments the underlying iterator value twice is all you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!