• Register

Iteration is a term used in programming to mean repeating instructions. It applies them in a loop while programming by using FOR and WHILE statements. It is a single pass through a set of instructions. Iterations can only run with algorithms, which is the set of step-by-step instructions. 

Note: I give the methods for iterating set after explaining iteration. So jump if you need them.

Example

Clothes washing algorithm using washing machine:

  1. Sort clothes to wash 

  2. Set water temperature and the cycle 

  3. Add detergent 

  4. Washing 

  5. Dry 

Iteration depends on a simple algorithm like the one above to execute a command. We may repeat one process several times until told otherwise.

The two iterations include the FOR and WHILE loops.

FOR LOOP

C++ uses loops widely, and it is deterministic, which means the number of times a loop is running is already known. Example of syntax

for (initialize;condition;update)

{

//body of the loop

}

The loop initializes and executes the condition in one run.

WHILE LOOP

The loop applies where the number of iterations is not known. Example of syntax;

While(condition)

{

//body of while loop

}

The loop runs several times until the condition set by the programmer is met. 

Methods

  • using iterator

  • using reverse iterator

  • using range base for loop

Using iterator

In this method we need to iterate in a forward direction. So we have to create an iterator and also have to initialise it with set::begin().

Here set::begin() returns an iterator pointing to the first element or data in the set and set::end() returns iterator pointing past to the last element. We are initializing iterator with set::begin() so it can point to the start of the set and can increment iterator and can go forward till it reaches set::end().

// Creating a iterator pointing to start of set
std::set<std::string>::iterator it = setOfStr.begin();
// Iterate till the end of set
while (it != setOfStr.end())
{
    // Print the element
    std::cout << (*it) << " , ";
    //Increment the iterator
    it++;
}

Using reverse iterator

Reverse iterators are also used to iterate, but only if we are trying to iterate in the backward direction of the set. Similarly, we have to create an iterator to iterate over the set but reverse iterator this time. Iterator must need to initialise with set::rbegin().

Here set::rbegin() returns a reverse iterator pointing to the last element of set and set::rend() returns an iterator pointing to the element before the first.

we are initialising the iterator with set::rbegin() so it can point at the last element of the set and can go in the reverse direction of the set.

// Creating a reverse iterator pointing to end of set i.e. rbegin
std::set<std::string>::reverse_iterator revIt = setOfStr.rbegin();
// Iterate till the start of set i.e. rend
while (revIt != setOfStr.rend())
{
    // Print the element
    std::cout << (*revIt) << " , ";
    //Increment the iterator
    revIt++;
}

Using range base for loop

// Iterate over all elements of set
// using range based for loop
for (auto elem : setOfStr)
{
    std::cout << elem << " , ";
}

I hope this helps.

100 points
7 2