当前位置: 首页 > wzjs >正文

江门网站推广软件重庆百度关键词优化软件

江门网站推广软件,重庆百度关键词优化软件,网站 如何做 同时在线,上海广告公司大全并行滤波和滤波-还原 QtConcurrent::filter()、QtConcurrent::filtered() 和 QtConcurrent::filedReduced() 函数并行过滤序列中的项目,如QList 。QtConcurrent::filter() 就地修改序列,QtConcurrent::filtered() 返回包含过滤内容的新序列,…

并行滤波和滤波-还原

QtConcurrent::filter()、QtConcurrent::filtered() 和 QtConcurrent::filedReduced() 函数并行过滤序列中的项目,如QList 。QtConcurrent::filter() 就地修改序列,QtConcurrent::filtered() 返回包含过滤内容的新序列,QtConcurrent::filteredReduced() 返回单一结果。

这些函数是 Qt Concurrent框架的一部分。

上述每个函数都有一个阻塞变体,它返回最终结果而不是QFuture 。使用它们的方法与异步变体相同。

QStringList strings = ...;// each call blocks until the entire operation is finished
QStringList lowerCaseStrings = QtConcurrent::blockingFiltered(strings, allLowerCase);QtConcurrent::blockingFilter(strings, allLowerCase);QSet<QString> dictionary = QtConcurrent::blockingFilteredReduced(strings, allLowerCase, addToDictionary);

请注意,上述结果类型不是QFuture 对象,而是真正的结果类型(本例中为QStringList 和QSet<QString>)。

并发过滤器

QtConcurrent::filtered() 接收一个输入序列(序列等于容器)和一个过滤函数。然后,序列中的每个项都会调用该过滤函数,并返回一个包含过滤值的新序列。

过滤函数的形式必须是

bool function(const T &t);

T 必须与序列中存储的类型相匹配。如果项目应被保留,函数返回true ;如果应被丢弃,函数返回 false。

本例展示了如何从QStringList 中保留全小写字符串:

bool allLowerCase(const QString &string)
{return string.lowered() == string;
}QStringList strings = ...;
QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings, allLowerCase);

过滤器的结果可通过QFuture 获取。有关如何在应用程序中使用QFuture 的详细信息,请参阅QFuture 和QFutureWatcher 文档。

如果您想就地修改序列,请使用 QtConcurrent::filter():

QStringList strings = ...;
QFuture<void> future = QtConcurrent::filter(strings, allLowerCase);

由于序列是就地修改的,QtConcurrent::filter() 不会通过QFuture 返回任何结果。不过,您仍然可以使用QFuture 和QFutureWatcher 来监控过滤器的状态。

并发过滤-还原

QtConcurrent::filteredReduced() 类似于 QtConcurrent::filtered(),但不是返回包含过滤结果的序列,而是使用 reduce 函数将结果合并为一个值。

reduce 函数的形式必须是

V function(T &result, const U &intermediate)

T 是最终结果的类型,U 是被过滤项的类型。注意,不使用 reduce 函数的返回值和返回类型。

像这样调用 QtConcurrent::filteredReduced():

void addToDictionary(QSet<QString> &dictionary, const QString &string)
{dictionary.insert(string);
}QStringList strings = ...;
QFuture<QSet<QString>> dictionary = QtConcurrent::filteredReduced(strings, allLowerCase, addToDictionary);

对于过滤函数保留的每个结果,reduce 函数将被调用一次,并应将中间 结果合并到结果变量中。QtConcurrent::filteredReduced() 保证每次只有一个线程调用 reduce,因此无需使用互斥来锁定结果变量。QtConcurrent::ReduceOptions 枚举提供了一种控制还原顺序的方法。

其他 API 功能

使用迭代器代替序列

上述每个函数都有一个使用迭代器范围而非序列的变体。使用方法与序列变体相同:

QStringList strings = ...;
QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings.constBegin(), strings.constEnd(), allLowerCase);// filter in-place only works on non-const iterators
QFuture<void> future = QtConcurrent::filter(strings.begin(), strings.end(), allLowerCase);QFuture<QSet<QString>> dictionary = QtConcurrent::filteredReduced(strings.constBegin(), strings.constEnd(), allLowerCase, addToDictionary);

使用成员函数

QtConcurrent::filter()、QtConcurrent::filtered()和 QtConcurrent::filteredReduced() 接受指向成员函数的指针。成员函数类的类型必须与存储在序列中的类型相匹配:

// keep only images with an alpha channel
QList<QImage> images = ...;
QFuture<void> alphaImages = QtConcurrent::filter(images, &QImage::hasAlphaChannel);// retrieve gray scale images
QList<QImage> images = ...;
QFuture<QImage> grayscaleImages = QtConcurrent::filtered(images, &QImage::isGrayscale);// create a set of all printable characters
QList<QChar> characters = ...;
QFuture<QSet<QChar>> set = QtConcurrent::filteredReduced(characters, qOverload<>(&QChar::isPrint),qOverload<const QChar&>(&QSet<QChar>::insert));

注意qOverload 的使用。需要使用它来解决有多个重载的方法的歧义问题。

还请注意,在使用 QtConcurrent::filteredReduced() 时,您可以自由混合使用普通函数和成员函数:

// can mix normal functions and member functions with QtConcurrent::filteredReduced()// create a dictionary of all lower cased strings
extern bool allLowerCase(const QString &string);
QStringList strings = ...;
QFuture<QSet<QString>> lowerCase = QtConcurrent::filteredReduced(strings, allLowerCase,qOverload<const QString&>(&QSet<QString>::insert));// create a collage of all gray scale images
extern void addToCollage(QImage &collage, const QImage &grayscaleImage);
QList<QImage> images = ...;
QFuture<QImage> collage = QtConcurrent::filteredReduced(images, &QImage::isGrayscale, addToCollage);

使用函数对象

QtConcurrent::filter()、QtConcurrent::filtered()和 QtConcurrent::filteredReduced() 接受过滤函数的函数对象。这些函数对象可用于为函数调用添加状态:

struct StartsWith
{StartsWith(const QString &string): m_string(string) { }bool operator()(const QString &testString){return testString.startsWith(m_string);}QString m_string;
};QList<QString> strings = ...;
QFuture<QString> fooString = QtConcurrent::filtered(strings, StartsWith(QLatin1String("Foo")));

还原函数也支持函数对象:

struct StringTransform
{void operator()(QString &result, const QString &value);
};QFuture<QString> fooString =QtConcurrent::filteredReduced(strings, StartsWith(QLatin1String("Foo")), StringTransform());

使用 Lambda 表达式

QtConcurrent::filter()、QtConcurrent::filtered() 和 QtConcurrent::filteredReduced() 接受用于过滤和还原函数的 lambda 表达式:

// keep only even integers
QList<int> list { 1, 2, 3, 4 };
QtConcurrent::blockingFilter(list, [](int n) { return (n & 1) == 0; });// retrieve only even integers
QList<int> list2 { 1, 2, 3, 4 };
QFuture<int> future = QtConcurrent::filtered(list2, [](int x) {return (x & 1) == 0;
});
QList<int> results = future.results();// add up all even integers
QList<int> list3 { 1, 2, 3, 4 };
QFuture<int> sum = QtConcurrent::filteredReduced(list3,[](int x) {return (x & 1) == 0;},[](int &sum, int x) {sum += x;}
);

当使用 QtConcurrent::filteredReduced() 或 QtConcurrent::blockingFilteredReduced() 时,您可以自由混合使用普通函数、成员函数和 lambda 表达式

void intSumReduce(int &sum, int x)
{sum += x;
}QList<int> list { 1, 2, 3, 4 };
QFuture<int> sum = QtConcurrent::filteredReduced(list,[] (int x) {return (x & 1) == 0;},intSumReduce
);

您还可以将 lambda 传递给 reduce 对象:

bool keepEvenIntegers(int x)
{return (x & 1) == 0;
}QList<int> list { 1, 2, 3, 4 };
QFuture<int> sum = QtConcurrent::filteredReduced(list,keepEvenIntegers,[](int &sum, int x) {sum += x;}
);

封装包含多个参数的函数

如果要使用包含多个参数的过滤器函数,可以使用 lambda 函数或std::bind() 将其转换为包含一个参数的函数。

例如,我们使用QString::contains():

bool QString::contains(const QRegularExpression &regexp) const;

QString::contains() 包含 2 个参数(包括 "this "指针),不能直接与 QtConcurrent::filtered() 一起使用,因为 QtConcurrent::filtered() 期望使用一个包含一个参数的函数。要在 QtConcurrent::filtered() 中使用QString::contains() ,我们必须为regexp参数提供一个值:

QStringList strings = ...;
QFuture<QString> future = QtConcurrent::filtered(list, [](const QString &str) {return str.contains(QRegularExpression("^\\S+$")); // matches strings without whitespace
});

Concurrent Filter and Filter-Reduce | Qt Concurrent 6.8.2

http://www.dtcms.com/wzjs/416761.html

相关文章:

  • 北京网站设计制作关键词优化微信小程序开发推广网站测试
  • 重庆网站开发公司大地seo视频
  • 成品影视app开发月光宝盒怎么样seo基础篇
  • 南京网站模板免费网站收录网站推广
  • 网站建设的方案模板专门培训seo的网站
  • 提交图片的网站要怎么做nba西部排名
  • wordpress 非插件七牛cdn全站加速5000元网站seo推广
  • wordpress的文章中可以添加图片吗武汉seo招聘信息
  • 国外有什么好的网站百度问答优化
  • 政务系统网站建设性价比高seo的排名优化
  • 零售店铺管理系统太原seo网站优化
  • 网站建设全包公司推荐跨境电商哪个平台比较好
  • 关键词优化公司网站百度信息流账户搭建
  • 搜索排名怎么做泰州seo
  • 做php网站开发能赚钱吗整合营销传播最基础的形式是
  • 做网站好幽默广告软文案例
  • 阿凡达网站建设东莞市网站seo内容优化
  • 如何发布自己做的网站域名查询ip地址
  • 生成论坛网站百度资讯
  • 石家庄做网站wsjz苏州seo免费咨询
  • 手机网站和电脑网站开发seo点击器
  • 十堰学网站建设培训班青岛网站建设与设计制作
  • 泉州seo 泉州网站建设百度推广代理商加盟
  • 佛山seo扣费百度网站排名优化软件
  • 做网站用到的java技术seo培训网
  • 门户网站建设方案内容神马快速排名优化工具
  • 营销策划公司排名攀枝花seo
  • 网站地址怎么做超链接站长工具综合查询
  • 潮州网站设计谷歌paypal下载
  • 建行个人余额查询系统官网营销推广seo