主要作用是筛选出符合条件的项值结果集,并与之替换原有序列列表
可以直接修改容器的每一项
不直接修改容器的每一项,而是将处理后的结果返回一个新的容器
ResultType QtConcurrent::blockingMappedReduced(const Sequence &sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions = ReduceOptions(UnorderedReduce | SequentialReduce))
Calls mapFunction once for each item in sequence. The return value of each mapFunction is passed to reduceFunction.
Note that while mapFunction is called concurrently, only one thread at a time will call reduceFunction. The order in which reduceFunction is called is determined by reduceOptions.
按顺序为每个项目调用mapFunction一次。每个mapFunction的返回值被传递给reduceFunction。
请注意,当mapFunction被并发调用时,一次只有一个线程会调用reduceFunction。reduceFunction的调用顺序由reduceOptions决定。
#include
#include
#include
int Addfunc(const int &arg)
{
return arg + 1;
};
int multiplyFunc(const int &num)
{
return num * 10;
}
void reducedFunction(int &result, const int &intermedia)
{
result += intermedia;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//<1>blockingFilter 主要作用是筛选出符合条件的项值结果集,并与之替换原有序列列表
QList<int> list0{2, 7, 6, 5, 10, 9, 8, 3, 4};
QtConcurrent::blockingFilter(list0, [](int v) { return v > 5; });
qDebug() << "blockingFilter sequence=" << list0;//(7, 6, 10, 9, 8)
//<2>blockingMap 可以直接修改容器的每一项
QList<int> list1 = {1, 2, 3, 4, 5};
QtConcurrent::blockingMap(list1, [](int &num){num = num * 10;});
qDebug() << "blockingMap list1=" << list1;//(10, 20, 30, 40, 50)
//<3>blockingMapped 不直接修改容器的每一项,而是将处理后的结果返回一个新的容器
QList<int> list2 = {3, 4, 5, 6, 7};
list2 = QtConcurrent::blockingMapped(list2, multiplyFunc);
qDebug() << "blockingMapped list2=" << list2;//(30, 40, 50, 60, 70)
//<4>按顺序为每个项目调用multiplyFunc一次,每个multiplyFunc的返回值被传递给reducedFunction
QList<int> list3 = {6, 7, 8, 9, 10};
int res = QtConcurrent::blockingMappedReduced(list3, multiplyFunc, reducedFunction);
qDebug() << "blockingMappedReduced res=" << res;//400
//--------------------------Asynchronous call--------------------------
#if 1
//QtConcurrent::map
QList<int> sequence0{1, 2, 3, 4, 5, 6, 7};
QFuture<void> future0 = QtConcurrent::map(sequence0, Addfunc);
QFutureWatcher<void> *watcher = new QFutureWatcher<void>();
QObject::connect(watcher, &QFutureWatcher<void>::finished, [=](){
qDebug() << "QtConcurrent::map" << sequence0;
});
//QtConcurrent::mapped
QList<int> sequence1{1, 2, 3, 4, 5, 6, 7};
QFuture<int> future1 = QtConcurrent::mapped(sequence1, multiplyFunc);
qDebug() << "QtConcurrent::mapped" << future1.results();//(10, 20, 30, 40, 50, 60, 70)
//新的结果用QFuture::const_iterator或QFutureIterator进行访问
QFutureIterator<int> itr(future1);
while (itr.hasNext()) {
qDebug() << "itr=" << itr.next();
}
//QtConcurrent::mappedReduced()类似于mapped()
//区别在于将结果继续传递给一个新函数,并在新函数里再处理成一个单值
QList<int> sequence2{1, 2, 3, 4, 5, 6, 7};
QFuture<int> future2 = QtConcurrent::mappedReduced(sequence2, multiplyFunc, reducedFunction);
future2.waitForFinished();
int result = future2.result();
qDebug() << "QtConcurrent::mappedReduced" << result;//280
#endif
return a.exec();
}