Convert QList to std::vector

Convert QList to std::vector

When working with Qt applications, developers often encounter the need to interface with standard C++ libraries. One common challenge is converting data structures between Qt's own containers and those from the standard template library (STL). This tutorial explains how to convert QList to std::vector.

The code initialize a QList with three elements. Then, a std::vector is created using the constructor that accepts iterators, which enables the direct conversion from the contents of QList. Finally, a loop iterates through each element and prints it to the standard output.

#include <QList>
#include <iostream>

int main()
{
    QList qlist({"First", "Second", "Third"});
    std::vector list(qlist.constBegin(), qlist.constEnd());

    for (auto &item: list) {
        std::cout << item << std::endl;
    }

    return 0;
}

Leave a Comment

Cancel reply

Your email address will not be published.