views:

606

answers:

4

What advantages are there in accessing vector elements using an iterator vs an index?

+4  A: 

Modularity is the answer. Suppose you wrap your logic in a function call (a good practice). In that case making it receive iterator will make it generic, so that it can operate on a C style array (pointer), an C++ stl vector or anything really that behaves like an iterator container, such as a linked list for instance.

David Reis
+1  A: 

This question was asked by me recently regarding performance. You might want to take a look at the answers I got.

Check the following link: Iterators vs. indexes

The discussion was mainly about performance, which turned out to be platform dependant, with minor changes in each platform.

Gal Goldman
Would you say then the advantages of iterators is it's pointer like operation the few methods it is provided with?
Babiker
This is not what I'm saying, I referred only to the performance aspect.
Gal Goldman
+1 Even though the question was about advantages with iterators. But iterators DO cause overhead. And it's very noticeable.
Magnus Skog
+5  A: 

Why are iterators better than indexes?

  • In the cases where index is not available (like with std::list, for example).
  • In the case where a generic function accepting an iterator is called.
  • When writing a function template that is supposed to work with more than one container type.
  • They exist to create uniformity among all containers and ability to use all containers' iterators as well as regular pointers in all standard algorithms.
  • Iterators can point to sequences that don't exist except as a concept. For instance, you can make an iterator class that steps through prime numbers without actually having to build a container of primes.

However, if ignoring container types that do not support random access (list, set, etc.), iterators still offer

  • Pointer like semantics (think of string::iterator vs. char *).
  • Generalized concept usable beyond iteration over elements inside a container.
  • Better performance than container member functions in a few cases.
nhaa123
I agree, and particularly I'd put the accent on a very practical advantage: cycling across a vector's elements using iterators is error safe for off-by-one situations.More simply, using iterators for vectors is somehow size-agnostic.
Scarlet
As long as you need sequential access, you're right. In other cases, there's no uniformity.
xtofl
+3  A: 

I say its portability across containers.

If you write a code using vectors and use index to iterate then the code cannot be changed to other containers easily later .

typedef std::vector<int> myContainer; //only change here for std::list 

for ( myContainer::iterator iter = actualContainer.begin();
      iter != actualContainer.end(); 
      ++iter)
{}

In the above code if you want to change from vector to list, it is very easily possible. If you had used the index then it won't be possible.

Otherwise since the vector uses random access iterators it should be the same. ( index or iterator anything is ok)

aJ