The fact that I have vector elements, I donโt care about them . Than I have N indices (each addressing a unique position in a vector) of elements that should be removed from the vector. I want the removal to be as quick as possible.
The best I could come up with was to store indexes in a set (order indices):
std::set<unsigned int> idxs; for (int i=0; i<N; ++i) idxs.insert(some_index);
And than iterating over the set in the reverse order and replacing the index with the deletion over the last element of the vector.
std::set<unsigned int>::reverse_iterator rit; for (rit = idxs.rbegin(); rit != idxs.rend(); ++rit) { vec[*rit].swap(vec[vec.size() - 1]); vec.resize(vec.size() - 1); }
However, I was wondering if there is an even more efficient way to do this, since using the set seems a bit overkill for me, and I would like to avoid sorting into everything.
EDIT1: Suppose I use a vector and sort it afterwards.
std::vector<unsigned int> idxs; for (int i=0; i<N; ++i) idxs.push_back(some_index); std::sort(idxs.begin(), idxs.end());
Can i click on next?
EDIT2: I should have mentioned that a vector will contain up to 10 elements. However, deletion in my program happens very often (hundreds of thousands of times).
source share