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. [SOLVED] operator [] or QList::at()?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] operator [] or QList::at()?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.6k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    isaacEnrique
    wrote on 25 Aug 2014, 01:00 last edited by
    #1

    Greetings.

    I wonder if using the operator [] on a constant QList (or QVector) causes a deep copy.

    I have a function that receives as a parameter a const QList< cv::Mat >& and l want to know if it is better access each element with the operator [] or the member function QList::at(). As far as I read this last function can be faster than operator [], but for a constant container I'm not sure if the performance is the same in both cases.

    On matters of style I prefer to use the operator [], but I'm not sure which is the better option in this situation?

    Thanks in advance for any help and/or suggestion.

    Isaac Pérez
    Programming is understanding.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 25 Aug 2014, 05:29 last edited by
      #2

      Check out the documentation: the constant variant is the same as QList::at(). "Link":http://qt-project.org/doc/qt-5/qlist.html#operator-5b-5d-2.

      (Z(:^

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jeroentjehome
        wrote on 25 Aug 2014, 09:23 last edited by
        #3

        Hi,
        When accessing QList members it's good to know that most Qt classes use 'copy at write' operations with a reference counter. This said the performance between [] or at() might be slim for const use (no writable). So Qt will give a reference to the QList member and not a deep copy!

        Greetz, Jeroen

        1 Reply Last reply
        0
        • I Offline
          I Offline
          isaacEnrique
          wrote on 26 Aug 2014, 01:54 last edited by
          #4

          Thank you all.

          So in summary for this case, both options (operator [] and QList::at()) have the same or similar performance... I am right?

          Isaac Pérez
          Programming is understanding.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 26 Aug 2014, 05:45 last edited by
            #5

            Yes, you are, as long as we are talking about const containers. In non-const onces, operator[] can insert a new default-constructed value when an item at given index is not present, thus copying the data.

            (Z(:^

            1 Reply Last reply
            0

            1/5

            25 Aug 2014, 01:00

            • Login

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