Tomáš Zato
Tomáš Zato

Reputation: 53190

How to create QList from std::vector

QList::fromStdList allows you to create QList from std::list. But how to create QList from std::vector?

Of course, aside from manual looping:

QList<T> myList;
myList.reserve(vector.size());
for(size_t i = 0, l = vector.size(); i < l; ++i)
    myList << vector[i];

Upvotes: 9

Views: 14581

Answers (1)

Christian G
Christian G

Reputation: 973

QVector can be created from a std vector, so you could do this if you want to avoid a loop:

QList<T> myList = QList<T>::fromVector(QVector<T>::fromStdVector(vector));

Of course, this would create an unnecessary copy in the process. Instead of having to write a loop, you could also use a std::copy and back_inserter

QList<T> myList;
myList.reserve(vector.size());
std::copy(vector.begin(), vector.end(), std::back_inserter(myList));

Upvotes: 23

Related Questions