mercredi 1 juillet 2015

Transform and accumulate with C++11

Coming from python I wounder if C++ nowadays has some good functional things there are in Python.

Currently I am trying to figure out how to produce a sum of some elements using indexes instead. For example with Python I would:

a = [ ... ]
b = [ ... ]

sum(map(lambda i: a[i] * 2 - b[i + 1], range(len(a)))

So here I generate a new array where each element is determined by some index arithmetic.

In STL there are transform and accumulate, but they take iterators as arguments. This would be ok if only on each step I needed to change a concrete element. Here I need to use index.

Also with transform I have to specify the array I need to put the results to. Is there a way the resulting array could be generated on the fly and then returned? Something like python's map does.

Because in such case it's really easier to write a simple for-loop.

Aucun commentaire:

Enregistrer un commentaire