3

With a random access iterator, you can change the stride length by simply doing iter+=n and then using < container.end() instead of != container.end() as the loop ending condition:

#include <iostream>
#include <vector>

int main(int argc, char *argv[])
{
  typedef std::vector<float> VectorType;
  typedef VectorType::const_iterator IteratorType;

  VectorType v;
  for(unsigned int i = 0; i < 11; ++i)
  {
    v.push_back(i);
  }

  for(IteratorType iter = v.begin(); iter < v.end(); iter += 2)
  {
    std::cout << " " << *iter;
  }

  return 0;
}

However both += 2 and < iter.end() seem to be undefined for something like std::set. It seems reasonable to want traverse a set only visiting every other element (subsampling it), no? Is there another way to do this?

1
  • I'm on an iPad, so I can't experiment with the code at the minute, but I'm just wondering if you've tried this using a non const iterator?
    – user206705
    Commented Feb 22, 2012 at 0:46

2 Answers 2

4

With a random access iterator, you can change the stride length by simply doing iter+=n and then using < container.end() instead of != container.end() as the loop ending condition

Actually, you cannot. While the code may compile, it exhibits undefined behavior at runtime if the iterator is actually advanced past the end of the container. You cannot increment an iterator beyond the end of the range into which it points.

In any case, you can write a function template to help:

template <typename TForwardIt, typename TDifference>
bool try_advance(TForwardIt& it,
                 TForwardIt const end,
                 TDifference n)
{
    TDifference i(0);
    while (i < n && it != end)
    {
        ++i;
        ++it;
    }

    return i == n;
}
2
  • If you made this call one of two functions, one taking random access and one taking other iterators, that'd be better for vectors and such. Commented Feb 22, 2012 at 1:15
  • @James McNellis: Cool, thanks. I used it like this: IteratorType iter = v.begin(); while(try_advance(iter, v.end(), 2)) { std::cout << " " << *iter; } What was the idea of TDifference? why not just: template <typename TForwardIt> bool try_advance(TForwardIt& it, TForwardIt const end, const unsigned int n) { unsigned int i = 0; while (i < n && it != end) { ++i; ++it; } return i == n; } ? Commented Feb 22, 2012 at 12:42
2

I'm on an iPad, so I can't test this, but try the following

std::advance(iter, 2);
3
  • 1
    +1. It does advance it two positions ... however the problem of moving past the end of the container pointed out by @James still exists. Commented Feb 22, 2012 at 0:55
  • Ah yes, this is definitely the solution to part 1 :) Commented Feb 22, 2012 at 0:59
  • Could be used in conjunction with bounds / pointer checks, hardly elegant tho... :)
    – user206705
    Commented Feb 22, 2012 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.