Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
This is the answer of exercise #7: AddOne.
The answer is a product of the number of ways to add one to an int and the number of ways to iterate through a std::vector.
The number of ways to add one to an int are five:
The number of ways to iterate through a std::vector are twelve:


Using a for-loop, requesting the std::vector its size only once

Using a for-loop, requesting the std::vector its size every iteration

Using an iterator, requesting the std::vector its end only once

Using an iterator, requesting the std::vector its end only every iteration

Using an algorithm to a non-inlined function

Using an algorithm to an inlined function

Using an algorithm to a functor with a non-inlined operator()

Using an algorithm to a functor with an inlined operator()

Using BOOST_FOREACH

Using the Boost Lambda library

Using the C++11 its ranged for-loop

Using the C++11 lambda expressions
This makes the number of ways to do this sixty!
There are two more:
Total: there are sixty-two ways to add one to each element in a std::vector!
P.S. an additional way (though beyond this exercise) is to use assembler.
This kind of program is called a benchmark.
There are too many ways to write one, so I'll just show you mine:
In the zip file, the results of the profilers gprof and cachegrind are included as text files.
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.