Copying to stdout using STL

The STL (Standard Template Library) is a collection of generic algorithms and data structures. This little quiz demonstrates one of the many useful things one can achieve with just a few lines of code when utilizing the power of this library.

Question: What is the output of the following code?

Answer: 0 1 2 3 4 5 6 7 8 9


The function main() uses the generate algorithm to initialise the int array using functor g. This functor will be called for every element within the array, with the result of the call used to initialise that element. Once initialised, the whole array is copied to the standard output stream using the copy algorithm.

Tip: Most of STL makes good use of functors and there are some neat reusable generic algorithms, which can be used to make code simple and easier to maintain.