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 delete the last n items in a QSet
QtWS25 Last Chance

How to delete the last n items in a QSet

Scheduled Pinned Locked Moved Solved General and Desktop
qset
5 Posts 2 Posters 454 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.
  • O Offline
    O Offline
    odelaune
    wrote on 6 Oct 2023, 08:06 last edited by
    #1

    Hi,
    I use a QSet of QObject* such as QSet<QObject*> m_set

    To add new item in the QSet, I do m_set.insert(new QObject);

    I also want to delete item in the QSet depending on its size. So I have a function like this:

    while(m_set.size() > number)
            m_set.erase(std::prev(m_set.constEnd()));
    

    but I get a crash when I try to remove items from the QSet. What is wrong?

    C 1 Reply Last reply 6 Oct 2023, 08:16
    0
    • O odelaune
      6 Oct 2023, 08:23

      @Christian-Ehrlicher
      I understand there is no last element, so how should I use QSet::erase?

      If my QSet contains 3 items, I would like to keep only 1 of them (any of them). How could I do that?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 6 Oct 2023, 08:28 last edited by
      #4

      QSet::erase(QSet::begin())
      But you're using a wrong container for your task. Maybe take a look at QList or even better QCache.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      O 1 Reply Last reply 6 Oct 2023, 08:35
      3
      • O odelaune
        6 Oct 2023, 08:06

        Hi,
        I use a QSet of QObject* such as QSet<QObject*> m_set

        To add new item in the QSet, I do m_set.insert(new QObject);

        I also want to delete item in the QSet depending on its size. So I have a function like this:

        while(m_set.size() > number)
                m_set.erase(std::prev(m_set.constEnd()));
        

        but I get a crash when I try to remove items from the QSet. What is wrong?

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 6 Oct 2023, 08:16 last edited by
        #2

        Since QSet is unordered there is no 'last' . But this was already told you on stackoverflow iirc.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        O 1 Reply Last reply 6 Oct 2023, 08:23
        2
        • C Christian Ehrlicher
          6 Oct 2023, 08:16

          Since QSet is unordered there is no 'last' . But this was already told you on stackoverflow iirc.

          O Offline
          O Offline
          odelaune
          wrote on 6 Oct 2023, 08:23 last edited by odelaune 10 Jun 2023, 08:24
          #3

          @Christian-Ehrlicher
          I understand there is no last element, so how should I use QSet::erase?

          If my QSet contains 3 items, I would like to keep only 1 of them (any of them). How could I do that?

          C 1 Reply Last reply 6 Oct 2023, 08:28
          0
          • O odelaune
            6 Oct 2023, 08:23

            @Christian-Ehrlicher
            I understand there is no last element, so how should I use QSet::erase?

            If my QSet contains 3 items, I would like to keep only 1 of them (any of them). How could I do that?

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 6 Oct 2023, 08:28 last edited by
            #4

            QSet::erase(QSet::begin())
            But you're using a wrong container for your task. Maybe take a look at QList or even better QCache.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            O 1 Reply Last reply 6 Oct 2023, 08:35
            3
            • C Christian Ehrlicher
              6 Oct 2023, 08:28

              QSet::erase(QSet::begin())
              But you're using a wrong container for your task. Maybe take a look at QList or even better QCache.

              O Offline
              O Offline
              odelaune
              wrote on 6 Oct 2023, 08:35 last edited by
              #5

              @Christian-Ehrlicher

              Thanks, it works with

              while(m_set.size() > number)
                      m_set.erase(m_set.constBegin());
              
              1 Reply Last reply
              0
              • O odelaune has marked this topic as solved on 6 Oct 2023, 08:35

              5/5

              6 Oct 2023, 08:35

              • Login

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