Difference between Iterators and Pointers in C++ with Examples

Last Updated : 27 Jun, 2026

In C++, both pointers and iterators are used to access and traverse data. They share several similarities, such as supporting dereferencing and increment operations, but they serve different purposes. Pointers directly manipulate memory addresses, whereas iterators provide a generalized way to traverse elements in containers.

  • Pointers provide direct access to memory locations.
  • Iterators provide a container-independent mechanism to access and traverse elements.

Pointer

A pointer is a variable is a variable that stores the memory address of another object.

  • Provides direct access to memory locations.
  • Commonly used for arrays, dynamic memory allocation, and low-level programming.

Example: The below example demonstrates the use of pointers to store the address of a variable.

C++
// The output of this program can be differen 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;
}

Output
Value of x is: 5
address of x is: 0x7ffde9197ed4

Explanation: The pointer ptr stores the address of the variable x. Dereferencing the pointer using *ptr gives access to the value stored at that memory location.

Iterator

An iterator is an object used to access and traverse elements stored in STL containers.

  • Provides a generic way to traverse different container types.
  • Works seamlessly with STL algorithms and container operations.

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;
}

Output
Without iterators = 1 2 3 
With iterators = 1 2 3 
Without iterators = 1 2 3 4 
With iterators = 1 2 3 4 

Explanation: The program demonstrates traversing a vector both with array indexing and with iterators. After inserting a new element using push_back(), both methods are used again to access the updated contents of the vector.

Difference between Iterators and Pointers

Although iterators and pointers support similar operations such as dereferencing and incrementing, they differ in functionality and usage.

FeaturePointersIterators
DefinitionStore the memory address of another objectProvide an abstraction for traversing container elements
Memory AccessDirectly access memory locationsAccess elements through container interfaces
FlexibilityWork with raw memory and arraysWork with STL containers
Arithmetic OperationsSupport pointer arithmetic (+, -, ++, --)Supported only by specific iterator categories
Type RestrictionsCan point to any object of a compatible typeUsually restricted to elements of a particular container
Memory ManagementCan be deleted using deleteCannot be deleted directly
SafetyProne to invalid memory accessSafer because container manages memory
UsageUsed in low-level programming and dynamic memory managementUsed for container traversal and STL algorithms
PerformanceUsually faster due to direct memory accessMay introduce slight abstraction overhead

Advantages of Iterators over Pointers

Iterators provide several advantages when working with STL containers:

  • Work with different container types through a common interface.
  • Integrate seamlessly with STL algorithms such as sort(), find(), and count().
  • Support different traversal capabilities through iterator categories.
  • Hide implementation details of the underlying container.
Comment