【发布时间】:2016-06-02 06:36:53
【问题描述】:
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 框架是否提供了这么好的算法?
【问题讨论】:
-
为什么要让 Qt 复制 STL 算法?只需在 Qt 容器(例如
QVector)上使用std::find_if,它就可以正常工作。还有QVector::indexOf和其他一些,我没有找到find_if的版本。 -
是的。我也是。我在问自己,如果我无法在文档中正确搜索。也许我真的必须在
QVector和std::vector之间来回转换。 -
@FrankSimon 为什么要将 / 转换为
std::vector?您可以使用QVector直接致电std::find_if。 -
啊。现在我明白了。
QVector接口与STL兼容?这真是巧妙。 -
值得一提的是,Qt 早先提供了与 STL 算法等效的 doc.qt.io/qt-5/qtalgorithms.html#details,但从 Qt 5.0 开始鼓励直接使用 STL 实现
标签: c++ qt stl containers