gpt4 book ai didi

c++ - 如何将 QtConcurrent 映射到 QVector 的 QList 上并减少为一个 QVector

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:49:03 26 4
gpt4 key购买 nike

为了完成这项工作,我真的尝试结合我所知道的一切。相信目前的结构不会出现死锁或其他线程问题。然而,有些部分丢失了,可用的文档对我没有帮助(我没有使用 C++ 文档的经验)。

int main() 
{
QVector<double> vector_of_doubles(5);
qFill(vector_of_doubles.begin(), vector_of_doubles.end(), 1);

QList< QVector<double> > list_of_vectors;
list_of_vectors.append(vector_of_doubles);
list_of_vectors.append(vector_of_doubles);
list_of_vectors.append(vector_of_doubles);

QFuture< QVector<double> > res;
res = QtConcurrent::mappedReduced(list_of_vectors, Map(), Reduce());
res.waitForFinished();
qDebug()<<res.result();
return 0
}

QVector<double> Reduce(QVector<double> vector)
// Here the vectors get combined into one big vector....but how?
{
....
return big_vector;
}

QVector<double> Map(QVector<double> vector)
{
for (int i = 0; i < vector.size(); i++)
{
vector[i] = vector[i] + i;
}
return vector;
}

我想从包含 3 个 vector 的 QList 的输入进入一个 vector ,每个 vector 都添加了一些迭代变量。我希望结果是 qDebug()看:

{1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4 ,5}

这里我认为是缺失的部分:

  • QtConcurrent:mappedReduced() 怎么样? , 我给出的论据是否正确?
  • 返回的函数呢,应该怎么整理?
  • 到底需要包含什么,我只需要包含 QtConcurrent 吗?
  • 一旦开始工作,列表会很大,据我所知,QtConcurrent 将进行线程管理(使用可用内核),并且列表中的所有项目(即使大小不同)都将被传递聪明地让线程不要让它们空闲?

编辑(也许是点击按钮时发生的问题?):

虽然它适用于我的示例,但是当我使用它时可能会出现什么问题:

扼流圈:res = QtConcurrent::mappedReduced(holder, correlate, Reduce, QtConcurrent::SequentialReduce);

变量:QList< QVector<double> > holder;

涉及的函数:

QVector<double> correlate(QVector<double> &var1); 

void Reduce(QVector<double> &reduceResult, const QVector<double> &partial)`

错误:没有匹配函数来调用 'mappedReduced(QList<QVector<double> >&, <unresolved overloaded function type>, <unresolved overloaded function type>, QtConcurrent::ReduceOption)'

还有:“无法推断模板参数 ResultType”

它与控制台应用程序中的工裁剪是一回事。

最佳答案

是的,QtConcurrent 的文档很糟糕。 Here is更好地描述应该如何使用 mappedReduced。

所以 reduce 函数应该是这样的:

void joinVectors(QVector<double> &reduceResult, const QVector<double> &partial) {
reduceResult += partial;
}

为了保持正确的数据顺序,您应该添加额外的参数:

res = QtConcurrent::mappedReduced(list_of_vectors, doSomeMapping, joinVectors, QtConcurrent::SequentialReduce);


这是我用于测试的代码,它可以正常工作(成功构建并给出预期结果):

#include <QCoreApplication>
#include <QtConcurrent/QtConcurrent>
#include <QDebug>

QVector<double> addIndexToVector(QVector<double> vector) {
for (int i = 0; i < vector.size(); i++) {
vector[i] = vector[i] + i;
}
return vector;
}

void joinVectors(QVector<double> &reduceResult, const QVector<double> &partial) {
reduceResult += partial;
}

int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QVector<double> vector_of_doubles(20, 0);

QList< QVector<double> > list_of_vectors;
list_of_vectors.append(vector_of_doubles);
list_of_vectors.append(vector_of_doubles);
list_of_vectors.append(vector_of_doubles);

QFuture< QVector<double> > res;
res = QtConcurrent::mappedReduced(list_of_vectors, addIndexToVector, joinVectors);
res.waitForFinished();
qDebug()<<res.result();

return a.exec();
}

项目文件:

QT       += core concurrent
QT -= gui
TARGET = testApp
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

我用的是Qt 5.0.1 Linux版。

关于c++ - 如何将 QtConcurrent 映射到 QVector<double> 的 QList 上并减少为一个 QVector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18559224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com