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

    Christian EhrlicherC 1 Reply Last reply
    0
    • O odelaune

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

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      3
      • O odelaune

        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?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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
        2
        • Christian EhrlicherC Christian Ehrlicher

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

          O Offline
          O Offline
          odelaune
          wrote on last edited by odelaune
          #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?

          Christian EhrlicherC 1 Reply Last reply
          0
          • O odelaune

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

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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
            3
            • Christian EhrlicherC Christian Ehrlicher

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

              • Login

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