what is difference between pointer and iterator
Pointer vs iterator
Hi @Namanjain123
Iterators and pointers are similar in that we can dereference them to get a value.
However, there are key differences as follows:
-
A pointer hold an address in memory.An iterator may hold a pointer, but it may be something much more complex. For example, an iterator can iterate over data that’s on file system, spread across many machines, or generated locally in a programmatic fashion. A good example is an iterator over linked list, the iterator will move through elements that are at nodes in the list whose addresses in RAM may be scattered.
-
We can perform simple arithmetic on pointers like increment, decrement, add an integer etc. Not all iterators allow these operations, e.g., we cannot decrement a forward-iterator, or add an integer to a nonrandom-access iterator.
-
A pointer of type T* can point to any type T object.An iterator is more restricted, e.g., a vector::iterator can only refer to doubles that are inside a vector container.
-
We can delete a pointer using delete. Since an iterator refers to objects in a container, unlike pointers, there’s no concept of delete for an iterator. (The container is responsible for memory management.)