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. How to remove certain portion of elements(QList)
QtWS25 Last Chance

How to remove certain portion of elements(QList)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 6.5k 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.
  • E Offline
    E Offline
    Engelard
    wrote on 21 Nov 2018, 22:59 last edited by Engelard
    #1

    I need some function which would allow me do something like that:

    items.removeFromTo(157, 2877);    //remove all elements between 157 and 2877
    

    I need to use such a function numerous times, so do that manually for every element through loop worst idea.

    J 1 Reply Last reply 21 Nov 2018, 23:06
    0
    • E Offline
      E Offline
      Engelard
      wrote on 21 Nov 2018, 23:04 last edited by
      #2

      Found one: erase(from, to);

      1 Reply Last reply
      3
      • E Engelard
        21 Nov 2018, 22:59

        I need some function which would allow me do something like that:

        items.removeFromTo(157, 2877);    //remove all elements between 157 and 2877
        

        I need to use such a function numerous times, so do that manually for every element through loop worst idea.

        J Offline
        J Offline
        JonB
        wrote on 21 Nov 2018, 23:06 last edited by JonB
        #3

        @Engelard
        Well, QList does not have a "range delete" method. Items will have to be removed in a loop. I don't know why you need to do numerous range deletions, or whether you've actually timed these. Would QVector with its http://doc.qt.io/qt-5/qvector.html#remove-1 remove(int i, int count) have been a better choice? Or some other data structure?

        E 1 Reply Last reply 22 Nov 2018, 00:24
        0
        • J JonB
          21 Nov 2018, 23:06

          @Engelard
          Well, QList does not have a "range delete" method. Items will have to be removed in a loop. I don't know why you need to do numerous range deletions, or whether you've actually timed these. Would QVector with its http://doc.qt.io/qt-5/qvector.html#remove-1 remove(int i, int count) have been a better choice? Or some other data structure?

          E Offline
          E Offline
          Engelard
          wrote on 22 Nov 2018, 00:24 last edited by
          #4

          @JonB why then at doc in description:

          Removes all the items from begin up to (but not including) end. Returns an iterator to the same item that end referred to before the call.

          J 1 Reply Last reply 22 Nov 2018, 08:05
          0
          • E Engelard
            22 Nov 2018, 00:24

            @JonB why then at doc in description:

            Removes all the items from begin up to (but not including) end. Returns an iterator to the same item that end referred to before the call.

            J Offline
            J Offline
            JonB
            wrote on 22 Nov 2018, 08:05 last edited by
            #5

            @Engelard
            You are quite right! I spotted remove(), and noticed that QList did not offer a count overload, but did not occur to me to look for erase()!

            I still wonder though:

            so do that manually for every element through loop worst idea.

            Erasing from a list still requires visiting each element, so I would have thought that erase() will simply be of the same order as looping through each element anyway.

            1 Reply Last reply
            2

            1/5

            21 Nov 2018, 22:59

            • 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