What is the difference between erase, removeAll, removeOne, removeAt in QList?
opengpu last edited by
what is the difference between erase, removeAll, removeOne, removeAt in QList?
should i write delete p; p = NULL; before or after (erase, removeAll, removeOne, removeAt) operation?
Binary91 last edited by
To remove all occurrences of a specific string, you use QStringList::removeAll(QString);
To remove only the first occurence of a specific string, you use QStringList::removeOne(QString);
To remove only the first string ("Hello"), you use QStringList::removeFirst();
To remove a string at a specific position, you use QStringList::removeAt(int pos);
erease is only useful in combination with iterators, because it also removes items (i.e. the iterator's item) and returns the iterator for the next item that is now at the position of the currently removed item...
Let's take a look at a QStringList (QList with strings) like this:
list << "Hello" << "I'm" << "a" << "list" << "Hello";
list.removeAll("Hello"); // list now: "I'm a list";
list.removeOne("Hello"); // list now: "I'm a list Hello";
list.removeFirst(); // list now: "I'm a list Hello";
list.removeAt(3); // list now: "Hello I'm a Hello";@
Only removing those items doesn't delete them, since they were allocated dynamically, so yes, AFAIK you still have to delete them. So the better way would be removing and getting its pointer with takeFirst(), takeAt(int pos), takeLast()...
delete list.takeLast(); // removes and deletes the last QPushButton;
To remove and delete all items, you'll have to do it with a loop:
Hope that helps ;-)