Pointer: A pointer is a variable which contains the address of another variable, i.e., address of the memory location of the variable. Like any variable or constant, we must declare a pointer before using it to store any variable address.
Iterator: An iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range.
type_container :: iterator var_name;
Without iterators = 1 2 3 With iterators = 1 2 3 Without iterators = 1 2 3 4 With iterators = 1 2 3 4
Differnece between Iterators and Pointers:
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.)|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Const vs Regular iterators in C++ with examples
- Difference between pointer to an array and array of pointers
- Iterators in C++ STL
- Random-access Iterators in C++
- Forward Iterators in C++
- Output Iterators in C++
- Bidirectional Iterators in C++
- Introduction to Iterators in C++
- How to iterate through a Vector without using Iterators in C++
- Different types of range-based for loop iterators in C++
- Input Iterators in C++
- Pointers in C/C++ with Examples
- Chain of Pointers in C with Examples
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Introduction of Smart Pointers in C++ and It's Types
- Dangling, Void , Null and Wild Pointers
- What are near, far and huge pointers?
- Features and Use of Pointers in C/C++
- Pointers and References in C++
- Why C treats array parameters as pointers?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : hp21