Qt容器类的通用搜索算法(Generic Search Algorithms for Qt container classes)

系统教程 行业动态 更新时间:2024-06-14 17:02:17
Qt容器类的通用搜索算法(Generic Search Algorithms for Qt container classes)

STL提供了各种函数来查找容器类中的元素。 Qt 5.5容器类中是否有类似的函数,例如QList或QVector ?

特别是,我正在寻找一个等效的单行,即使用Qt容器和Qt算法的std::find_if :

int main(int arg, char** args) { std::vector<int> c = { 2,3,4,6,6,15 }; if (std::find_if(c.begin(), c.end(), [](const int& value) { return value % 5 == 0; }) != c.end()) { std::cout << "At least one element divisible by 5." << std::endl; } else { std::cout << "No element is divisible by 5." << std::endl; } return 0; }

元素可被5整除的谓词应该只是作为一个例子。

Qt框架是否提供了这么好的算法?

The STL overs a variety of functions to find elements in container classes. Are there similar functions in for Qt 5.5 container classes e.g. QList or QVector?

Especially, I'm looking for an equivalent one-liner i.e. std::find_if using Qt containers and Qt algorithms:

int main(int arg, char** args) { std::vector<int> c = { 2,3,4,6,6,15 }; if (std::find_if(c.begin(), c.end(), [](const int& value) { return value % 5 == 0; }) != c.end()) { std::cout << "At least one element divisible by 5." << std::endl; } else { std::cout << "No element is divisible by 5." << std::endl; } return 0; }

The predicate of an element being divisible by 5 should be just serve as an example.

Does the Qt Framework provides such nice algorithms?

最满意答案

algorithm头中定义的STL算法可以与Qt容器一起使用。 如果Qt缺少等效算法,则没有理由避免使用STL算法。 如果Qt是使用STL支持构建的,它应该默认工作。

#include <algorithm> // std::find_if #include <QApplication> #include <QVector> int main(int argc, char *argv[]) { QApplication app(argc, argv); QVector<int> c{ 2,3,4,6,6,15 }; if (std::find_if(c.begin(), c.end(), [](const int& value) { return value % 5 == 0; }) != c.end()) { ... } return app.exec(); }

STL algorithms defined in algorithm header can be used with Qt containers. If Qt lacks an equivalent algorithm there is no reason to avoid using the STL algorithm. If Qt is built with STL support it should work by default.

#include <algorithm> // std::find_if #include <QApplication> #include <QVector> int main(int argc, char *argv[]) { QApplication app(argc, argv); QVector<int> c{ 2,3,4,6,6,15 }; if (std::find_if(c.begin(), c.end(), [](const int& value) { return value % 5 == 0; }) != c.end()) { ... } return app.exec(); }

更多推荐

本文发布于:2023-04-21 18:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a8fa4ba407ad88087df2e5a0811b9d2c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   容器   Generic   Qt   container

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!