STL覆盖了多种功能以在容器类中查找元素。Qt 5.5容器类中是否有类似的功能,例如QList
或QVector
?
特别是,我正在寻找一种等效的单线方式,即std::find_if
使用Qt容器和Qt算法:
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 Framework是否提供了如此出色的算法?
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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句