- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 QtConcurrent 并行运行某个任务。但是,当我决定改为使用 mappedReduced 而不是 map 时,我遇到了问题。
这是我使用的代码:
struct ProcMapWrapper {
ConcurrentProcessTask *instance;
ProcMapWrapper(ConcurrentProcessTask *w): instance(w) {}
QString & operator()(const QString& data) {
return instance->map(data);
}
ProcMapWrapper(const ProcMapWrapper & src)
{
instance = src.instance;
}
typedef QString result_type;
};
struct ProcReduceWrapper {
ConcurrentProcessTask *instance;
ProcReduceWrapper(ConcurrentProcessTask *w): instance(w) {}
void operator()(int &number, const QString &fname) {
return instance->reduce(number,fname);
}
ProcReduceWrapper(const ProcReduceWrapper & src)
{
instance = src.instance;
}
typedef int result_type;
};
ProcMapWrapper mw(this);
ProcReduceWrapper rw(this);
futureWatcher.setFuture(QtConcurrent::mappedReduced(_files.begin(),_files.end(),mw,rw));
想法是使用 processOneItem 函数从输入字符串生成 stage1Data 结构,然后在 reduce 函数中清除并将结果保存在适当的容器中。
我做了一些研究,使我找到了 this source file , 指的是 these code snippets .在那里,映射仿函数的模板如下:
struct Scaled
{
Scaled(int size)
: m_size(size) { }
typedef QImage result_type;
QImage operator()(const QImage &image)
{
return image.scaled(m_size, m_size);
}
int m_size;
};
QList<QImage> images = ...;
QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100));
这确实适用于 QtConcurrent 的 map
和 mapped
函数,但是我尝试使用相同的模式制作 reduce 仿函数再次导致这些错误:
..\qt_photoaud\task.cpp(230) : error C2893: Failed to specialize function template 'QFuture::ResultType> QtConcurrent::mappedReduced(Iterator,Iterator,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' With the following template arguments: 'QList::iterator' with [ T=QString ] 'ProcMapWrapper' 'ProcReduceWrapper' ..\qt_photoaud\task.cpp(230) : error C2783: 'QFuture QtConcurrent::mappedReduced(Iterator,Iterator,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'ResultType' c:\qt\4.8.4\include\qtcore../../src/corelib/concurrent/qtconcurrentmap.h(152) : see declaration of 'QtConcurrent::mappedReduced' ..\qt_photoaud\task.cpp(230) : error C2893: Failed to specialize function template 'QFuture::ResultType> QtConcurrent::mappedReduced(const Sequence &,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' With the following template arguments: 'QList::iterator' with [ T=QString ] 'QList::iterator' with [ T=QString ] 'ProcMapWrapper' ..\qt_photoaud\task.cpp(230) : error C2783: 'QFuture QtConcurrent::mappedReduced(const Sequence &,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'ResultType' c:\qt\4.8.4\include\qtcore../../src/corelib/concurrent/qtconcurrentmap.h(125) : see declaration of 'QtConcurrent::mappedReduced'
reduce functor 真的被定义和/或识别了吗? QtPrivate::ReduceResultType::ResultType 是如何工作的?它需要什么?
编辑:假设仿函数与 boost::function 兼容,我尝试在 reduce 仿函数中指定 first_argument_type、second_argument_type、arg1_type 和 arg2_type。但这没有任何帮助。
最佳答案
这是对我有用的:
struct concurrentProcessDBMapper
{
typedef stage1Data result_type;
stage1Data operator()(QString fname)
{
return concurrentProcessDBMap(fname);
}
};
struct concurrentProcessDBReducer
{
DatabaseConcurrentProcessTask *instance;
concurrentProcessDBReducer(DatabaseConcurrentProcessTask *i)
{
instance = i;
}
void operator()(int & number, const stage1Data in)
{
//concurrentProcessDBReduce
instance->reduce(number,in);
}
};
...
QFutureWatcher<int> futureWatcher;
QList<QString> _filesInWork;
...
futureWatcher.setFuture(
QtConcurrent::mappedReduced
<int, QList<QString>, concurrentProcessDBMapper,concurrentProcessDBReducer>
(_filesInWork,concurrentProcessDBMapper(),concurrentProcessDBReducer(this)));
...
总而言之,随着类型转换像 -(map)->-(reduce)-> 一样,map 仿函数将 result_type 定义为 stage1Data,而 reduce 仿函数根本没有定义任何 result_type。 Future watcher 将其类型定义为 reduce 结果(即,其第一个输出参数的类型)——在我的例子中是 int——mappedReduced 的模板参数是明确定义的。
关于c++ - 为 mappedReduce 指定 reduce 仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019954/
我是一名优秀的程序员,十分优秀!