which container (Qvector or QList) is good to use for insert, delete, and access the last element.
-
I am adding data point at last position of container, and only accessing the last element of container. So which is better container?
I am curious to know what is time complexity of QList::last(), O(n) or O(1)?
If I am not wrong then doubly link list: O(1) and singly link list has O(n). -
@Yash001 said in which container (Qvector or QList) is good to use for insert, delete, and access the last element.:
I am curious to know what is time complexity of QList::last(), O(n) or O(1)?
I think you'll find it's O(1). By inspection of https://code.woboq.org/qt5/qtbase/src/corelib/tools/qlist.h.html. For example
inline void **end() const noexcept { return d->array + d->end; } T& last() { Q_ASSERT(!isEmpty()); return *(--end()); }
QVector
will surely be O(1), and if @guerinoni suggests that's the way to go for the future he probably knows what he is talking about :) -
@Yash001 said in which container (Qvector or QList) is good to use for insert, delete, and access the last element.:
I am adding data point at last position of container, and only accessing the last element of container. So which is better container?
It's what we call Last In First Out ( LIFO) queue.
So QStack seems an obvious candidate.QStack inherits from QVector.