Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. which container (Qvector or QList) is good to use for insert, delete, and access the last element.
QtWS25 Last Chance

which container (Qvector or QList) is good to use for insert, delete, and access the last element.

Scheduled Pinned Locked Moved Solved General and Desktop
datastructuresqlistqvectorc++
5 Posts 5 Posters 877 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yash001
    wrote on last edited by
    #1

    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).

    guerinoniG JonBJ M 3 Replies Last reply
    0
    • Y Yash001

      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).

      guerinoniG Offline
      guerinoniG Offline
      guerinoni
      wrote on last edited by
      #2

      @Yash001 QVector is better, in Qt6 QList is merged into QVector

      1 Reply Last reply
      3
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        QLinkedList?

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        1
        • Y Yash001

          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).

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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 :)

          1 Reply Last reply
          2
          • Y Yash001

            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).

            M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            @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.

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved