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.Syntax:
type* var_name;
Example:
C++
#include <iostream>
int main()
{
int x;
std::cout << &x;
return 0;
}
C
#include <stdio.h>
int main()
{
int x;
printf("%p", &x;);
return 0;
}
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. Syntax:
type_container :: iterator var_name;
Example:
CPP
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v = { 1, 2, 3 };
vector<int>::iterator i;
int j;
cout << "Without iterators = ";
for (j = 0; j < 3; ++j) {
cout << v[j] << " ";
}
cout << "\nWith iterators = ";
for (i = v.begin(); i != v.end(); ++i) {
cout << *i << " ";
}
v.push_back(4);
cout << "\nWithout iterators = ";
for (j = 0; j < 4; ++j) {
cout << v[j] << " ";
}
cout << "\nWith iterators = ";
for (i = v.begin(); i != v.end(); ++i) {
cout << *i << " ";
}
return 0;
}
Output:
Without iterators = 1 2 3
With iterators = 1 2 3
Without iterators = 1 2 3 4
With iterators = 1 2 3 4
Difference 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:
Pointers
Iterators
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.)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
16 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...