How to remove certain portion of elements(QList)
-
wrote on 21 Nov 2018, 22:59 last edited by 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.
-
wrote on 21 Nov 2018, 23:04 last edited by
Found one: erase(from, to);
-
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.
wrote on 21 Nov 2018, 23:06 last edited by 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. WouldQVector
with its http://doc.qt.io/qt-5/qvector.html#remove-1remove(int i, int count)
have been a better choice? Or some other data structure? -
@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. WouldQVector
with its http://doc.qt.io/qt-5/qvector.html#remove-1remove(int i, int count)
have been a better choice? Or some other data structure? -
@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.
wrote on 22 Nov 2018, 08:05 last edited by@Engelard
You are quite right! I spottedremove()
, and noticed thatQList
did not offer acount
overload, but did not occur to me to look forerase()
!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/5