C++ Qt容器类的通用搜索算法

C++ Qt容器类的通用搜索算法,c++,qt,stl,containers,C++,Qt,Stl,Containers,STL覆盖了各种函数来查找容器类中的元素。Qt5.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) {

STL覆盖了各种函数来查找容器类中的元素。Qt5.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;
}
int-main(int-arg,char**args){
向量c={2,3,4,6,6,15};
if(std::find_if(c.begin(),c.end(),[](const int&value){返回值%5==0;})!=c.end(){

std::cout在
算法
标题中定义的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();
}
\include//std::find#u if
#包括
#包括
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QVector c{2,3,4,6,6,15};
if(std::find_if(c.begin(),c.end(),[](const int&value){返回值%5==0;})!=c.end(){
...
}
返回app.exec();
}

标准C++算法是指满足给定算法要求的容器。相关标准容器在这里并不特别,它们恰好满足要求。就像QT容器一样,还有很多其他容器。您应该编写自己的容器或迭代器适配器。在您的工作过程中,特别是利用标准算法


例如,您可以为or使用迭代器适配器,或者必须创建。

为什么要让Qt复制STL算法?只需在Qt容器(如
QVector
)上使用
std::find_if
,就可以了。有
QVector::indexOf
和其他一些工具,我没有找到
find_if
版本。是的。我也是it’他问我自己,如果我不能在文档中正确搜索。也许我真的必须在
QVector
std::vector
@FrankSimon之间来回转换。你为什么要从
std::vector
转换到
de>QVector
接口与
STL
兼容,这真的很有创意。值得注意的是,Qt早期提供了STL算法的等价物,但从Qt 5.0开始,直接使用STL实现是值得鼓励的。这里的其他人已经指出了这一点。请参阅评论。感谢回复。我已经回答了d一个有效的解决方案,非常简单。