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)

How to remove certain portion of elements(QList)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 7.3k 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.
  • EngelardE Offline
    EngelardE Offline
    Engelard
    wrote on 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.

    JonBJ 1 Reply Last reply
    0
    • EngelardE Offline
      EngelardE Offline
      Engelard
      wrote on last edited by
      #2

      Found one: erase(from, to);

      1 Reply Last reply
      3
      • EngelardE Engelard

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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?

        EngelardE 1 Reply Last reply
        0
        • JonBJ JonB

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

          EngelardE Offline
          EngelardE Offline
          Engelard
          wrote on 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.

          JonBJ 1 Reply Last reply
          0
          • EngelardE Engelard

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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

            • Login

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