Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What is the time complexity of QVector::toList() method?



  • Does QVector()::toList() produce a new QList in O(n) by iterating and copying elements, or is this perhaps an O(1) operation? My testing indicates that this is a linear time conversion. Am I correct?

    In general, is there documentation available regarding complexities for other related conversion operations, like fromList(), toStdVector() etc.?



  • QVector()::toList(), QList::toVector and the fromList/Vector() methods are all copy element by element of one to the other. It's trivial to check: https://code.woboq.org/qt5/include/qt/QtCore/qvector.h.html#_ZNK7QVector6toListEv

    QVector::toStdVector can leverage the fact that they both use contiguous memory and it boils down to little more than a memcpy

    Some methods/classes declare their complexity in the description but I would say it's the exception rather than the norm


Log in to reply