Add section on iterator-based loops (#33423)

This commit is contained in:
Terry Lewis
2019-03-21 12:50:21 +11:00
committed by Christopher McCormack
parent 4b2219b01d
commit 0b964091c9

View File

@ -15,18 +15,16 @@ For loop is an entry controlled loop unlike do-while loop.
## Syntax ## Syntax
``` ```c++
for (init; condition; increment ) for (init; condition; increment ) {
{
update_statement(s); update_statement(s);
} }
``` ```
The increment can also placed inside the for loop i.e. in its body- The increment can also placed inside the for loop i.e. in its body-
``` ```c++
for ( init; condition;) for ( init; condition;) {
{
update_statement(s); update_statement(s);
increment; increment;
} }
@ -34,10 +32,9 @@ for ( init; condition;)
It is also allowed to ignore the init variables if and only if they are declared beforehand. For example : It is also allowed to ignore the init variables if and only if they are declared beforehand. For example :
``` ```c++
int a = 1; int a = 1;
for (; a <= 10 ;) for (; a <= 10 ;) {
{
cout << a << '\n'; cout << a << '\n';
a++; a++;
} }
@ -61,7 +58,7 @@ The update statement is used to alter the loop variable by using simple operatio
You will often see an increment operation as the update statement (e.g. i++, count++). This is often seen as one of the distinguishing features and possible name sources for the C++ language. You will often see an increment operation as the update statement (e.g. i++, count++). This is often seen as one of the distinguishing features and possible name sources for the C++ language.
## Implementation ## Implementation
```C++ ```c++
#include <iostream> #include <iostream>
using std::cout; // Here we use the scope resolution operator to define the scope of the standard functions as std using std::cout; // Here we use the scope resolution operator to define the scope of the standard functions as std
using std::endl; using std::endl;
@ -132,14 +129,14 @@ C++ also has what we call "range-based" `for` loops which iterate through all th
### Syntax ### Syntax
```c++ ```c++
for ( element: container ) for ( element: container ) {
statement(s); statement(s);
} }
``` ```
```c++ ```c++
int[5] array = { 1, 2, 3, 4, 5 } int[5] array = { 1, 2, 3, 4, 5 }
for ( int i: array ) for ( int i: array ) {
cout << i << endl; cout << i << endl;
} }
``` ```
@ -153,14 +150,36 @@ Output:
5 5
``` ```
## Iterator-based for-loop
Iterator based for loops are also possible in C++ and functionality for them exists in many of the data structures found within the STL. Unlike for-each loops, iterator based loops allow for mutating the contents of the container during iteration. This is rather useful when one needs to remove or insert values while looping over data.
### Syntax
```c++
// Create a vector
std::vector<int> vec;
// Populate the vector
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
// Iterate over the vector using the 'it' object.
for(std::vector<string>::iterator it = vec.begin(); it != vec.end(); it++) {
// Print the value held by the iterator (this is the object or primitive contained within
// the vector, in this case, an int).
cout<< *it << endl; // prints d.
}
```
## Applications of the for loops ## Applications of the for loops
### Use as infinite loops ### Use as infinite loops
This C-style for-loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer. In fact, when infinite loops are intended, this type of for-loop can be used (with empty expressions), such as: This C-style for-loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer. In fact, when infinite loops are intended, this type of for-loop can be used (with empty expressions), such as:
``` ```c++
for (;;) for (;;) {
//loop body //loop body
}
``` ```
## Additional Resources ## Additional Resources