I have my loop going through vector’s elements. While in this loop some of the elements are being (I want them to be) removed. Although std::vector does not allow to do this, and I would like an alternative.
for(unsigned int j = 0; j < rectArray.size(); j++)
{
if( rectArray[j] == 2 )
{
rectArray.erase(rectArray.begin() + j);
}
//...
}
Do you think a std::list would be good here ? Can I use something else ?
I would suggest modifying your code such that it uses iterators instead of the actual vector. It’s much cleaner and more efficient like this:
Note that
autois a C++11 feature (the way I used it). If your compiler supports that you might also want to use C++11’s foreach: