Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between accessing vector elements using an iterator vs an index?

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

like image 802
Babiker Avatar asked May 28 '09 06:05

Babiker


People also ask

Are iterators faster than indexing?

Depending on the actual containercontainerIn computer science, a container is a class or a data structure whose instances are collections of other objects. In other words, they store objects in an organized way that follows specific access rules. The size of the container depends on the number of objects (elements) it contains.https://en.wikipedia.org › wiki › Container_(abstract_data_type)Container (abstract data type) - Wikipedia, incrementing an iterator might be faster than indexing (think linked lists).

How do you access the element of a vector using iterator?

Use an iteratorvector<int>::iterator iter; An iterator is used as a pointer to iterate through a sequence such as a string or vector . The pointer can then be incremented to access the next element in the sequence.

What is an iterator in vector?

Vector's iterators are random access iterators which means they look and feel like plain pointers. You can access the nth element by adding n to the iterator returned from the container's begin() method, or you can use operator [] . std::vector<int> vec(10); std::vector<int>::iterator it = vec.

What is the advantage of iterator in C++?

The main advantage of an iterator is to provide a common interface for all the containers type. Iterators make the algorithm independent of the type of the container used. Iterators provide a generic approach to navigate through the elements of a container.


3 Answers

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.
like image 191
nhaa123 Avatar answered Oct 14 '22 18:10

nhaa123


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.

like image 4
David Reis Avatar answered Oct 14 '22 18:10

David Reis


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)

like image 3
aJ. Avatar answered Oct 14 '22 17:10

aJ.