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++
#include <iostream>
#include <vector>
using namespace std;
void normal_iterator(vector< int > my_iterable)
{
cout << "Value before modification: " ;
for ( int my_iterator : my_iterable) {
cout << my_iterator << " " ;
}
for ( int my_iterator : my_iterable) {
my_iterator += 1;
}
cout << "\nValue after modification : " ;
for ( int my_iterator : my_iterable) {
cout << my_iterator << " " ;
}
}
int main()
{
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++
#include <iostream>
#include <vector>
using namespace std;
void reference_iterator(vector< int > my_iterable)
{
cout << "Value before modification: " ;
for ( int my_iterator : my_iterable) {
cout << my_iterator << " " ;
}
for ( int & my_iterator : my_iterable) {
my_iterator += 1;
}
cout << "\nValue after modification : " ;
for ( int my_iterator : my_iterable) {
cout << my_iterator << " " ;
}
}
int main()
{
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++
#include <iostream>
#include <vector>
using namespace std;
void reference_iterator(vector< int > my_iterable)
{
for ( const int & my_iterator : my_iterable) {
cout << my_iterator << " " ;
}
}
int main()
{
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;
}
|