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, apart from the manual loop :

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

      

+3


source to share


1 answer


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

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

      



Of course, this will create an unnecessary copy in the process. Instead of writing a loop, you can also use std :: copy and back_inserter

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

      

+8


source







All Articles