5

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?

5
  • 6
    Why do you want Qt to duplicate the STL algorithms? Just use std::find_if on Qt containers such as QVector, it works fine. There is QVector::indexOf and some others, I didn't find a find_if version.
    – nwp
    Commented Jun 2, 2016 at 6:42
  • Yes. Me too. I was asking myself, if I just wasn't able to correctly search in the docs. Maybe I really have to convert back and forth between QVector and std::vector.
    – Aleph0
    Commented Jun 2, 2016 at 6:44
  • 2
    @FrankSimon Why do you want convert from / to std::vector? You can call std::find_if directly with a QVector.
    – Holt
    Commented Jun 2, 2016 at 6:45
  • Ah. Now I got it. The QVector interface is compatible with the STL ? That is really ingenious.
    – Aleph0
    Commented Jun 2, 2016 at 6:46
  • 1
    It may worth a note that Qt earlier provided equivalents of STL algorithms, doc.qt.io/qt-5/qtalgorithms.html#details, but starting from Qt 5.0 using of STL implementations directly is encouraged
    – demonplus
    Commented Jun 2, 2016 at 9:54

2 Answers 2

7

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();
}
1
  • The other people here already pointed that out. See the comments. Thanks for the reply. I already found a working solution, it's really easy.
    – Aleph0
    Commented Jun 2, 2016 at 7:18
1

The standard C++ algorithms are meant to work with any container that fulfills the requirements of a given algorithm. The relevant standard containers are not special here: they happen to fulfill the requirements. Just as Qt containers do, and plenty of other containers. You'd be expected to write your own containers or iterator adapters in the course of your work anyway, specifically to leverage the standard algorithms.

For example, you can have an iterator adapter for QGraphicsScene or QLayout, or have to create an iterator for circular data structures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.