Difference between Iterators and Pointers in C++ with Examples
Last Updated :
09 Jan, 2024
In C++ programming, we have both pointers and iterators that are used in managing and manipulating data structures. There are many similarities between iterators and pointers in their ability to reference and dereference memory, but there are certain differences between the two. Understanding the difference is very important in C++ programming.
Pointer
A pointer is a variable that contains the address of another variable, i.e., the 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
The below example demonstrates the use of pointers to store the address of a variable.
C++
// The output of this program can be different
// in different runs. Note that the program
// prints address of a variable and a variable
// can be assigned different address in different
// runs.
#include <iostream>;
using namespace std;
int main()
{
int x=5;
int* myptr = &x;
cout << "Value of x is: " << x << endl;
cout << "address of x is: " << myptr << endl;
return 0;
}
OutputValue of x is: 5
address of x is: 0x7ffde9197ed4
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
The below example demonstrates the use of iterators.
C++
// C++ program to demonstrate iterators
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Declaring a vector
vector<int> v = { 1, 2, 3 };
// Declaring an iterator
vector<int>::iterator i;
int j;
cout << "Without iterators = ";
// Accessing the elements without using iterators
for (j = 0; j < 3; ++j) {
cout << v[j] << " ";
}
cout << "\nWith iterators = ";
// Accessing the elements using iterators
for (i = v.begin(); i != v.end(); ++i) {
cout << *i << " ";
}
// Adding one more element to vector
v.push_back(4);
cout << "\nWithout iterators = ";
// Accessing the elements without using iterators
for (j = 0; j < 4; ++j) {
cout << v[j] << " ";
}
cout << "\nWith iterators = ";
// Accessing the elements using iterators
for (i = v.begin(); i != v.end(); ++i) {
cout << *i << " ";
}
return 0;
}
OutputWithout 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.) |
Similar Reads
Difference Between Forward List and List in C++ Forward List is a sequence container that allows unidirectional sequential access to its data. It contains data of the same type. In STL, it has been implemented using Singly Linked List, which requires constant time for insertion and deletion. Elements of the forward list are scattered in the memor
3 min read
Difference between pointer to an array and array of pointers Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whol
4 min read
Difference between int* p() and int (*p)()? A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, a pointer must be declare before storing any variable address. The general form of a pointer variable declaration is: Syntax: type *var_name; Here, type
2 min read
Forward List and List of Pairs in C++ with Examples Forward List Forward list in STL implements singly linked list. Introduced from C++11, forward lists are more useful than other containers in insertion, removal, and moving operations (like sort) and allow time constant insertion and removal of elements. It differs from the list by the fact that the
8 min read
Difference between std::set::upper_bound and std::upper_bound in C++ Prerequisites: Random-access Iterators, Bidirectional Iterators Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and
4 min read