4

This was a question on my exam and the answer is that all pointers are iterators but not all iterators are pointers. Why is this the case?

In a statement such as:

int *p = new int(4);

How can p be considered an iterator at all?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
StarckOverflar
  • 1,577
  • 2
  • 10
  • 13
  • 2
    A pointer is a pointer. An iterator is an iterator. A pointer to some element in an array meets all the requirements of a random access iterator. Other pointers, ...not so much. – Sam Varshavchik Dec 17 '16 at 03:05
  • @SamVarshavchik My thoughts exactly, which I do not understand why all pointers are iterators – StarckOverflar Dec 17 '16 at 03:05
  • 1
    A blanket statement that "all pointers are iterators" is false. – Sam Varshavchik Dec 17 '16 at 03:06
  • A pointer to some object can be used like a pointer to the first element of an array with one element, so all pointers are iterators I guess. – Baum mit Augen Dec 17 '16 at 03:07
  • 1
    Besides, iterator just requires some semantics, not every iterator need be a valid iterator. – Baum mit Augen Dec 17 '16 at 03:08
  • Is it possible for there to be iterators that are not pointers? – StarckOverflar Dec 17 '16 at 03:11
  • This is an awful exam question and to answer it would require the professor to explain exactly what they mean by "iterator." An example of an iterator that is not a pointer would be `std::list::iterator` which is not the same type as `T*` – Ryan Haining Dec 17 '16 at 04:50
  • Yes, that is a terrible exam question, and an even worse answer. Very few college teachers are actual programmers. Teachers are taught by teachers. It's a vish. (I have been both a computer science professor and a professional programmer. Been there and there. Done that and that) – Jive Dadson Dec 17 '16 at 08:21

1 Answers1

11

"Iterator" is some abstract concept, describing a certain set of operations a type must support with some specific semantics.

Pointers are iterators because they fulfill the concept iterator (and, even stronger, random access iterator), e.g. the operator++ to move to the next element and operator * to access the underlying element.

In your particular example, you get a standard iterator range with

[p, p+1)

which can be used for example in the standard algorithms, like any iterator pair. (It may not be particularly useful, but it is still valid.) The above holds true for all "valid" pointers, that is pointers that point to some object.

The converse implication however is false: For example, consider the std::list<T>::iterator. That is still an iterator, but it cannot be a pointer because it does not have an operator[].

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
  • Are there anyways in which an iterator can be implemented without a pointer? – StarckOverflar Dec 17 '16 at 03:22
  • 1
    @Bob What exactly do you mean by that? An iterator that is not a pointer? See the last paragraph of my answer. An iterator that cannot be implemented with the use of a pointer? Probably not, one can always drop some pointer that does nothing; and pointers already are in the strongest iterator category. – Baum mit Augen Dec 17 '16 at 03:25
  • It should be noted that without an implementation of `iterator_traits` for pointers, even pointers don't fully qualify as iterators under the most recent standards. – Mark Ransom Dec 17 '16 at 04:05
  • 1
    @MarkRansom But we do have that since C++98, so it's not an issue. – Baum mit Augen Dec 17 '16 at 04:07