Posts tagged C++

Wordcount in C++

:: C++

This is the standard example for MapReduce applications, counting the words in a file. Note that this snippet is not threaded nor distributed. It is just a curiosity: how to implement the same in C++, using the STL to avoid having any for loop!

Reduce in standard C++

:: C++

Accumulate instead of loop

You can traverse a C++ vector in many ways. Looping over a counter variable that goes from 0 to the vector size or taking an iterator from the vector’s begin to its end pointer positions are well-known ways to access its elements. When you are collapsing the elements into a single accumulator value (an operation usually called reduce in functional programming), you can use the std::accumulate function in the numeric header.