The Wayback Machine - https://web.archive.org/web/20240531223956/https://www.geeksforgeeks.org/const-vs-regular-iterators-in-c-with-examples/
Open In App

Const vs Regular iterators in C++ with examples

Last Updated : 18 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Iterators in STL

Iterators are objects similar to pointers which are used to iterate over a sequence and manipulate the container elements. The advantage of using an iterator is that it reduces the lines of code to a single statement as they allow us to manipulate the built-in arrays in the STL using pointers as iterators. An iterator can either be a constant or a non-constant/regular iterator.

Constant Iterators:

A const iterator points to an element of constant type which means the element which is being pointed to by a const_iterator can’t be modified. Though we can still update the iterator (i.e., the iterator can be incremented or decremented but the element it points to can not be changed). It can be used for access only, and can’t be used for modification. If we try to modify the value of the element using const iterator then it generates an error.

Regular Iterators:

A regular or non const_iterator points to an element inside the container and can be used to modify the element to which it is pointing. Regular Iterators play a critical role in connecting algorithm with containers along with the manipulation of data stored inside the containers. The most obvious form of a regular iterator is a pointer.
A pointer can point to elements in an array and can iterate through them using the increment operator(++). Each container type has a specific regular iterator type designed to iterate through its elements.

Below is a C++ program to demonstrate the difference in the working of the two iterators:

C++




// C++ program to demonstrate a regular
// and const_iterator
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
  
// Function that demonstrate regular
// iterators
void regularIterator(vector<int>& v)
{
    // Declare a regular iterator
    // to a vector
    vector<int>::iterator i;
  
    // Printing the elements of the
    // vector v using regular iterator
    for (i = v.begin(); i < v.end(); i++) {
  
        // Update elements of vector
        *i += 1;
        cout << *i << " ";
    }
}
  
// Function that demonstrate const
// iterators
void constIterator(vector<int>& v1)
{
    // Declare a const_itearor
    // to a vector
    vector<int>::const_iterator ci;
  
    // Printing the elements of the
    // vector v1 using regular iterator
    for (ci = v1.begin(); ci < v1.end(); ci++) {
  
        // Below line will throw an error
        // as we are trying to modify the
        // element to which const_iterator
        // is pointing
  
        //*ci += 1;
  
        cout << *ci << " ";
    }
}
  
// Driver Code
int main()
{
    // Declaring vectors
    vector<int> v = { 7, 2, 4 };
    vector<int> v1 = { 5, 7, 0 };
  
    // Demonstrate Regular iterator
    regularIterator(v);
  
    cout << endl;
  
    // Demonstrate Const iterator
    constIterator(v1);
    return 0;
}


Output:

8 3 5
5 7 0


Similar Reads

Difference between const int*, const int * const, and int const *
int const* int const* is pointer to constant integer This means that the variable being declared is a pointer, pointing to a constant integer. Effectively, this implies that the pointer is pointing to a value that shouldn't be changed. Const qualifier doesn't affect the pointer in this scenario so the pointer is allowed to point to some other addre
3 min read
Difference between const char *p, char * const p and const char * const p
Prerequisite: Pointers There is a lot of confusion when char, const, *, p are all used in different permutations and meanings change according to which is placed where. Following article focus on differentiation and usage of all of these. The qualifier const can be applied to the declaration of any variable to specify that its value will not be cha
3 min read
Difference between Iterators and Pointers in C++ with Examples
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. Poi
4 min read
How to iterate through a Vector without using Iterators in C++
Prerequisite: C++ STL, Iterators in C++ STL The iterator is not the only way to iterate through any STL container. There exists a better and efficient way to iterate through vector without using iterators. It can be iterated using the values stored in any container. Below is the syntax for the same for vectors: Syntax: for(auto itr : vector_name) E
4 min read
Different types of range-based for loop iterators in C++
Range-Based 'for' loops have been included in the language since C++11. It automatically iterates (loops) over the iterable (container). This is very efficient when used with the standard library container (as will be used in this article) as there will be no wrong access to memory outside the scope of the iterable. The loop will automatically star
5 min read
Erase Range of Elements From List Using Iterators in C++ STL
Prerequisites:List in C++Iterators in C++ A list is a type of container which requires the same properties as a doubly linked list. We can insert elements from either side of the list, but accessing elements with an index is not possible in the list. So, removing elements from the list is not an easy task. But, we have a method to remove multiple e
2 min read
Iterators in C++ STL
Prerequisite : Introduction to Iterators Iterators are used to point at the memory addresses of STL containers. They are primarily used in sequences of numbers, characters etc. They reduce the complexity and execution time of the program. Operations of iterators :- 1. begin() :- This function is used to return the beginning position of the containe
4 min read
Input Iterators in C++
After going through the template definition of various STL algorithms like std::find, std::equal, std::count, you must have found their template definition consisting of objects of type Input Iterator. So what are they and why are they used?Input iterators are one of the five main types of iterators present in the C++ Standard Library, others being
6 min read
Random Access Iterators in C++
After going through the template definition of various STL algorithms like std::nth_element, std::sort, you must have found their template definition consisting of objects of type Random-access Iterator. So what are they and why are they used?Random-access iterators are one of the five main types of iterators present in C++ Standard Library, others
6 min read
Forward Iterators in C++
After going through the template definition of various STL algorithms like std::search, std::search_n, std::lower_bound, you must have found their template definition consisting of objects of type Forward Iterator. So what are they and why are they used ? Forward iterators are one of the five main types of iterators present in C++ Standard Library,
6 min read
Practice Tags :