Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-unit iterator stride with non-random access iterators

Tags:

c++

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?

like image 211
David Doria Avatar asked Dec 03 '25 02:12

David Doria


2 Answers

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;
}
like image 100
James McNellis Avatar answered Dec 05 '25 17:12

James McNellis


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

std::advance(iter, 2);

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!