Different types of range-based for loop iterators in C++
Last Updated :
16 Sep, 2021
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 start and end at the right place.
Syntax :
for ( range_declaration : range_expression )
loop_statement
There are three different types of range-based 'for' loops iterators, which are:
1. Normal Iterators:
In normal iterator, an ordinary temporary variable is declared as the iterator, and the iterator gets a copy of the current loop item by value. Any changes made to the temporary copy will not get reflected in the original iterable.
Syntax :
for (datatype iterator : list)
{
// operation are performed here
}
- The iterator used is a normal iterator of any data type like int, float, double, etc, which is used to iterate over any type of container.
- list can be any type of container.
Here is the implementation of the normal range based iterators :
C++
// C++ program to implements
// normal iterators
#include <iostream>
#include <vector>
using namespace std;
// Function to implements
// normal iterators
void normal_iterator(vector<int> my_iterable)
{
// Printing the iterable before making
// any changes
cout << "Value before modification: ";
for (int my_iterator : my_iterable) {
cout << my_iterator << " ";
}
// Case where the iterator
// makes a temporary copy
// of the current loop item
for (int my_iterator : my_iterable) {
// changing the value of the iterator
my_iterator += 1;
}
cout << "\nValue after modification : ";
// Printing the iterable
// to see if any changes
// has been made in the
// original container or not
for (int my_iterator : my_iterable) {
cout << my_iterator << " ";
}
}
// Driver Code
int main()
{
// Initialising a standard
// template container
vector<int> my_iterable;
my_iterable.push_back(101);
my_iterable.push_back(102);
my_iterable.push_back(103);
my_iterable.push_back(104);
normal_iterator(my_iterable);
return 0;
}
Output:Value before modification: 101 102 103 104
Value after modification : 101 102 103 104
2. Reference Iterators :
Reference iterators are declared as a reference variable, and the iterator gets the value of the current item by reference. So the changes made inside the loop are definitely get affected in the original container itself.
Syntax :
for (datatype & iterator : list)
{
// operation are performed here
}
- The iterator used is a normal iterator of any data type like int, float, double, etc, which is used to iterate over any type of container.
- list can be any type of container.
Here is the implementation of the normal range based iterators :
C++
// C++ program to implements
// reference iterators
#include <iostream>
#include <vector>
using namespace std;
// Function to implements
// reference iterators
void reference_iterator(vector<int> my_iterable)
{
// Printing the iterable before
// making any changes
cout << "Value before modification: ";
for (int my_iterator : my_iterable) {
cout << my_iterator << " ";
}
// Iterating the container
// using reference iterator
// and updating the value
for (int& my_iterator : my_iterable) {
my_iterator += 1;
}
cout << "\nValue after modification : ";
for (int my_iterator : my_iterable) {
cout << my_iterator << " ";
}
}
// Driver Code
int main()
{
// Initialising a standard
// template container
vector<int> my_iterable;
my_iterable.push_back(101);
my_iterable.push_back(102);
my_iterable.push_back(103);
my_iterable.push_back(104);
reference_iterator(my_iterable);
return 0;
}
Output:Value before modification: 101 102 103 104
Value after modification : 102 103 104 105
3. Constant Iterators :
Constant iterators are declared as a reference to a constant and in this case, no copy of the current loop item will be made making the execution faster as compared to the above two cases. This is useful in cases where we don't want any accidental changes in the iterator value or if we are iterating over large items in a container. If we will try to modify the existing value then the compiler will show errors.
Syntax :
for (const datatype iterator : list)
{
// operation are performed here
}
- The iterator used is a normal iterator of any data type like int, float, double, etc, which is used to iterate over any type of container.
- list can be any type of container.
Here is the implementation of the normal range based iterators :
C++
// C++ program to implements
// constant iterators
#include <iostream>
#include <vector>
using namespace std;
// Function to implements
// constant iterators
void reference_iterator(vector<int> my_iterable)
{
// Printing the iterable
// using constant iterator
for (const int& my_iterator : my_iterable) {
cout << my_iterator << " ";
// Uncomment below line to see the error
// my_iterator += 1 ;
}
}
// Driver Code
int main()
{
// Initialising a standard
// template container
vector<int> my_iterable;
my_iterable.push_back(101);
my_iterable.push_back(102);
my_iterable.push_back(103);
my_iterable.push_back(104);
reference_iterator(my_iterable);
return 0;
}
Similar Reads
Different ways to iterate over a set in C++
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific order. Syntax: set<datatype> setname; Here,Datatype: Set can take any data type depending on the values, e.g. int, char, float, et
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 eas
2 min read
Range-Based for Loop in C++
In C++, the range-based for loop introduced in C++ 11 is a version of for loop that is able to iterate over a range. This range can be anything that is iteratable, such as arrays, strings and STL containers. It provides a more readable and concise syntax compared to traditional for loops.Let's take
3 min read
Range-Based for Loop in C++
In C++, the range-based for loop introduced in C++ 11 is a version of for loop that is able to iterate over a range. This range can be anything that is iteratable, such as arrays, strings and STL containers. It provides a more readable and concise syntax compared to traditional for loops.Let's take
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 di
4 min read
How to delete a range of values from the List using Iterator
Given a List, the task is to delete a range of values from this List using Iterator. Example: Input: list = [10 20 30 40 50 60 70 80 90], start_iterator = 3, end_iterator = 8 Output: 10 20 80 90 Input: list = [1 2 3 4 5] start_iterator = 1, end_iterator = 3 Output: 3 4 5 Approach: In this method, a
2 min read
Different Ways to Remove an Element From Set in C++ STL
Prerequisite: Set in C++ There are multiple ways to remove an element from the set. These are as follows: Removing an element by its valueRemoving an element by its indexRemoving an element by an iterator Example: Input set s={10, 20, 30, 40, 50} , value=40 // Removing 40 from the set Output set s={
3 min read
How to Insert a Range of Elements in a Set in C++ STL?
Prerequisites: Set in C++ Sets in C++ are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending. Syntax: set<datatype> set_name; Some Basic Func
2 min read
Different Ways to Insert Elements in Set in C++ STL
Prerequisites: Set in C++ The C++ Standard Template Library offers containers called Sets. It functions essentially in the same ways as a binary search tree and is used to store different elements in increasing/decreasing order. There are different methods to insert elements in the set as mentioned
3 min read
Difference between for and while loop in C, C++, Java
In C, C++, and Java, both for loop and while loop is used to repetitively execute a set of statements a specific number of times. However, there are differences in their declaration and control flow. Let's understand the basic differences between a for loop and a while loop. for Loop A for loop prov
5 min read