Home > Enterprise >  Why using != to compare iterator in loop condition
Why using != to compare iterator in loop condition

Time:10-26

In many examples regarding iterating over vectors, I noticed that often the not-equals operator is used to check whether the loop has reached the vector's end. Normally, I am using the lower-than operator in the loop condition. Hence, I am wondering, what is the rationale behind choosing !=?

Example:

std::vector<int> vec = {1, 2, 3, 4, 5};
for (auto iter = vec.begin(); iter != vec.end(); iter  ) {
  std::cout << *iter << " ";
}

CodePudding user response:

Because not all iterators support ordering but all can be compared for equality in O(1).

For example associative (ordered) containers use bidirectional iterators, which are defined as (cppreference):

template<class I>
  concept bidirectional_iterator =
    std::forward_iterator<I> &&
    std::derived_from</*ITER_CONCEPT*/<I>, std::bidirectional_iterator_tag> &&
    requires(I i) {
      { --i } -> std::same_as<I&>;
      { i-- } -> std::same_as<I>;
    };

std::forward_iterator can only be dereferenced, copied, ==, and incremented.

On the other hand, random access iterator used by std::vector requires std::totally_ordered<I>. So in your case you could write iter < vec.end();, but the code will become less generic. As long as you do not increment the iterator in the loop's body, you are safe anyway.

CodePudding user response:

As per my understanding,
iterator has an implementation inside the STLs which enables you to "iterate" over the elements. And using them, programmers do not have to code the logic to move to the next element, depending on the datastructure it is implemented in.

Said that, the elements are not always stored in memory in a sequence. So, in contrast to index they cannot be just compared as less than or greater than.

With vector, specifically, its your choice to use index while comparing the elements position. but same can not be said with confidence with other DS.

So, in your question, the implementation of iterator enables to span over all the elements by comparing whether last element is reached or not.

  • Related