How to delete the last n items in a QSet
-
Hi,
I use a QSet of QObject* such asQSet<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?
-
QSet::erase(QSet::begin())
But you're using a wrong container for your task. Maybe take a look at QList or even better QCache. -
Since QSet is unordered there is no 'last' . But this was already told you on stackoverflow iirc.
-
@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?
-
QSet::erase(QSet::begin())
But you're using a wrong container for your task. Maybe take a look at QList or even better QCache. -
Thanks, it works with
while(m_set.size() > number) m_set.erase(m_set.constBegin());
-