Site icon Learn C++

Traversing sequences without writing explicit loops

The posts General Loop Statements in Modern C++ and Range-for-statement in modern C++ cover ways to write explicit loops. But explicit loops can be tedious to write and, what is more important, – harder to read, because the resulting code requires to spend the extra time by others in order to understand what is going on in the explicit loop. As alternative, the C++ standard library provides algorithms (defined in <algorithm>) that takes a callback function as an argument to be called for each element of a sequence. But what the advantage of using standard algorithms instead of writing the explicit loops? The main advantage is that it saves the time of both authors and users of the code, because the algorithms provided by the C++ standard library are proven, well-designed, documented and commonly known which allow to express the intentions of the programmers clearly. As a rule, the more standard library facilities are used to write code, the easier it is to maintain that code in the future.

The most general and simple algorithm intended to traverse sequences is std::for_each. This algorithm just eliminate the need to write the explicit loop, for example:

[crayon-673fdbc7b5034394443269/]

The example above demonstrates how to modify the entire vector of integers, and print the result to the standard output without writing the loops explicitly. Iterators were used to specify the sequence. Although it seems like it looks good, this snippet can be improved. Actually, the function negate() transforms the vector of integers by negating each element. The C++ standard library provides the special algorithm std::transform and unary function object std::negate (defined in <functional>). std::transform applies the given function to each element of the input sequence and stores the returned value to the corresponding element of the output sequence. std::negate<T> just calls operator - (minus) on an instance of type T and returns the result. Hence, the example above could be rewritten as follow:

[crayon-673fdbc7b503c102678381/]

Looks more clear, isn’t it? The intention to transform the elements of the vector by negating each element is clearly expressed now.

Summarizing this post:

And thanks to the algorithms of the C++ standard library which eliminates the need to write the loops explicitly in many cases!

Exit mobile version