C++ Qt QtConcurrent::filteredReduced 来自 std::shared_ptr 的 QVector
C++ Qt QtConcurrent::filteredReduced from QVector of std::shared_ptr
我的 class Person
中有 vector
个 shared_ptrs
,看起来像:
QVector <std::shared_ptr<const Person>> vecOfPeople;
Person 的字段之一是 age
,我想用 QtConcurrent::filteredReduced
来计算,例如有多少人超过 50
,我发现很难理解该怎么做它。我有一个 bool
返回函数 isOver50
作为:
bool isOver50(std::shared_ptr<const Person> &person)
{
return person->getAge() > 50;
}
如果我理解得好,应该还有一个 reduction
函数,在我的代码中它看起来像:
void reduction(int &result, std::shared_ptr<const Person> &person)
{
result++;
}
最后,使用 filteredReduced
编码为:
QFuture<int> futureOver50 = QtConcurrent::filteredReduced(vecOfPeople, isOver50, reduction);
futureOver50.waitForFinished();
qDebug() << futureOver50.result();
这无法编译,我敢打赌 reduction
函数有问题,但我不知道它是什么。
The filter function must be of the form:
bool function(const T &t);
The reduce function must be of the form:
V function(T &result, const U &intermediate)
你的 shared_ptr
参数是非常量引用(即使指向的类型是常量),Qt 想要传递一个常量引用,导致编译错误。
相反,请考虑使用
bool isOver50(const std::shared_ptr<const Person> &person);
void reduction(int &result, const std::shared_ptr<const Person> &person);
以后,请尝试将实际的错误消息与您的问题一起提交,这样可以更快地诊断这些问题
我的 class Person
中有 vector
个 shared_ptrs
,看起来像:
QVector <std::shared_ptr<const Person>> vecOfPeople;
Person 的字段之一是 age
,我想用 QtConcurrent::filteredReduced
来计算,例如有多少人超过 50
,我发现很难理解该怎么做它。我有一个 bool
返回函数 isOver50
作为:
bool isOver50(std::shared_ptr<const Person> &person)
{
return person->getAge() > 50;
}
如果我理解得好,应该还有一个 reduction
函数,在我的代码中它看起来像:
void reduction(int &result, std::shared_ptr<const Person> &person)
{
result++;
}
最后,使用 filteredReduced
编码为:
QFuture<int> futureOver50 = QtConcurrent::filteredReduced(vecOfPeople, isOver50, reduction);
futureOver50.waitForFinished();
qDebug() << futureOver50.result();
这无法编译,我敢打赌 reduction
函数有问题,但我不知道它是什么。
The filter function must be of the form:
bool function(const T &t);
The reduce function must be of the form:
V function(T &result, const U &intermediate)
你的 shared_ptr
参数是非常量引用(即使指向的类型是常量),Qt 想要传递一个常量引用,导致编译错误。
相反,请考虑使用
bool isOver50(const std::shared_ptr<const Person> &person);
void reduction(int &result, const std::shared_ptr<const Person> &person);
以后,请尝试将实际的错误消息与您的问题一起提交,这样可以更快地诊断这些问题