You can use std::accumulate.
The goal of QtConcurrent mappedReduce is the apply an operation on all the items of container in a concurrent fashion and once done do the reduction.
In your case there's nothing to be done but the sum of the items so there's no need for mappedReduce.
Update:
Based on your comments here is an example using a custom class with the operator+
overloaded using a QList as container.
class MyClass
{
public:
MyClass(int x=0, int y=0):
_x(x),
_y(y)
{}
int x() const { return _x; }
int y() const { return _y; }
public:
int _x;
int _y;
};
MyClass operator+(const MyClass& left, const MyClass &right)
{
MyClass m;
m._x = left._x + right._x;
m._y = left._y + right._y;
return m;
}
Here is a version with std::accumulate
#include <QVector>
#include <QtDebug>
#include <numeric>
int main(int argc, char **argv)
{
Q_UNUSED(argc);
Q_UNUSED(argv);
QVector<MyClass> myClassList = {MyClass(12, 42), MyClass(23, 53)};
MyClass result = std::accumulate(myClassList.constBegin(), myClassList.constEnd(), MyClass());
qDebug() << result.x() << result.y();
return 0;
}
Update 2:
Based on @IlBeldus suggestion, here is the version were you use std::accumulate
with QtConcurrent::run
#include <QtDebug>
#include <QVector>
#include <QtConcurrent>
#include <numeric>
MyClass myAccumulate(const QVector<MyClass> &input)
{
return std::accumulate(input.constBegin(), input.constEnd(), MyClass());
}
int main(int argc, char **argv)
{
Q_UNUSED(argc);
Q_UNUSED(argv);
QVector<MyClass> myClassList = {MyClass(12, 42), MyClass(23, 53)};
QFuture<MyClass> future = QtConcurrent::run(myAccumulate, myClassList);
result = future.result();
qDebug() << result.x() << result.y();
return 0;
}