Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a vector, why prefer an iterator over a pointer?

Tags:

In Herb Sutter's When Is a Container Not a Container?, he shows an example of taking a pointer into a container:

  // Example 1: Is this code valid? safe? good?   //   vector<char> v;    // ...    char* p = &v[0];    // ... do something with *p ... 

Then follows it up with an "improvement":

  // Example 1(b): An improvement   //               (when it's possible)   //   vector<char> v;    // ...    vector<char>::iterator i = v.begin();    // ... do something with *i ... 

But doesn't really provide a convincing argument:

In general, it's not a bad guideline to prefer using iterators instead of pointers when you want to point at an object that's inside a container. After all, iterators are invalidated at mostly the same times and the same ways as pointers, and one reason that iterators exist is to provide a way to "point" at a contained object. So, if you have a choice, prefer to use iterators into containers.

Unfortunately, you can't always get the same effect with iterators that you can with pointers into a container. There are two main potential drawbacks to the iterator method, and when either applies we have to continue to use pointers:

  1. You can't always conveniently use an iterator where you can use a pointer. (See example below.)

  2. Using iterators might incur extra space and performance overhead, in cases where the iterator is an object and not just a bald pointer.

In the case of a vector, the iterator is just a RandomAccessIterator. For all intents and purposes this is a thin wrapper over a pointer. One implementation even acknowledges this:

   // This iterator adapter is 'normal' in the sense that it does not    // change the semantics of any of the operators of its iterator    // parameter.  Its primary purpose is to convert an iterator that is    // not a class, e.g. a pointer, into an iterator that is a class.    // The _Container parameter exists solely so that different containers    // using this template can instantiate different types, even if the    // _Iterator parameter is the same. 

Furthermore, the implementation stores a member value of type _Iterator, which is pointer or T*. In other words, just a pointer. Furthermore, the difference_type for such a type is std::ptrdiff_t and the operations defined are just thin wrappers (i.e., operator++ is ++_pointer, operator* is *_pointer) and so on.

Following Sutter's argument, this iterator class provides no benefits over pointers, only drawbacks. Am I correct?

like image 282
user5360395 Avatar asked Sep 21 '15 18:09

user5360395


People also ask

Why use iterators instead of pointers?

After all, iterators are invalidated at mostly the same times and the same ways as pointers, and one reason that iterators exist is to provide a way to "point" at a contained object. So, if you have a choice, prefer to use iterators into containers.

Is vector iterator a pointer?

An iterator is an object (like a pointer) that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location using them.

What is the use of iterator in vector C++?

An iterator is used to point to the memory address of the STL container classes. For better understanding, you can relate them with a pointer, to some extent. Iterators act as a bridge that connects algorithms to STL containers and allows the modifications of the data present inside the container.

Which statement about the difference between pointer and iterator is true?

Which of the following is a true statement about the difference between pointers and iterators? While pointers are variable that hold memory address, iterators are generic functions used to traverse containers. These function allows the programmer to implement read and write code as the container is traversed.


1 Answers

For vectors, in non-generic code, you're mostly correct.

The benefit is that you can pass a RandomAccessIterator to a whole bunch of algorithms no matter what container the iterator iterates, whether that container has contiguous storage (and thus pointer iterators) or not. It's an abstraction.

(This abstraction, among other things, allows implementations to swap out the basic pointer implementation for something a little more sexy, like range-checked iterators for debug use.)

It's generally considered to be a good habit to use iterators unless you really can't. After all, habit breeds consistency, and consistency leads to maintainability.

Iterators are also self-documenting in a way that pointers are not. What does a int* point to? No idea. What does an std::vector<int>::iterator point to? Aha…

Finally, they provide a measure a type safety — though such iterators may only be thin wrappers around pointers, they needn't be pointers: if an iterator is a distinct type rather than a type alias, then you won't be accidentally passing your iterator into places you didn't want it to go, or setting it to "NULL" accidentally.

I agree that Sutter's argument is about as convincing as most of his other arguments, i.e. not very.

like image 105
Lightness Races in Orbit Avatar answered Sep 29 '22 21:09

Lightness Races in Orbit