Tag Info

New answers tagged

0

You could avoid the 2nd pass by calling erase as you delete the element. You need to convert the index to an iterator and be sure you account for the fact that erasing items changes subsequent indexes int deleted = 0; for (int i=0; i < Count; i++) { if (Selected[i]) { delete items[i]; items.erase(items.begin()+i-deleted++); } }


7

Your algorithm is O(N²) - if you have 1000 elements in your vector and the first 100 are null, it will run the while loop 100 times, each repositioning up to 999 elements. In implementation, you would use two iterators, one which you read from and one which you write to. If the read element is null, do not write it, otherwise write it back and increment. ...



Top 50 recent answers are included