One more vector related question. I searched the erase-remove idiom document for the answer and I don't think I found my answer.
Given vector.erase(vector.begin(), vector.begin() + vector.size() / 2); why would the vector not be half the size after that statement?
Erase should take two iterators in a range and delete that range, to my understanding. The range is: The beginning and the midpoint. Erase it.
I've tried, as a test, just deleting from begin() to end(), and that works fine, so my assumption is that begin() + size() / 2 doesn't evaluate to an a proper iterator. . . ?
@Peter This is exactly what I thought as well. I even tried casting the division to int to potentially cut off any decimal portion, and it still isn't working as I expect.
The code is part of a bigger statement aimed at randomly erasing (killing) half of a population after it has reached critical mass, due to starvation.
@Peter Sorry, that's just a reference to an unseen function size() is actually just a wrapper for colony.size() in the class. I can replace it, and same result.
Are you sure the size is ever above the threshold? I noticed you have no space or anything after printing the size so you might want to make sure you are not printing a number right after so that it looks like the size is much bigger than it really is.
The casting is unnecessary since vector.size() returns a size_t (an unsigned value) and the 2 is an int, so you already have integer math.
And by the way is there a reason you're "erasing" the first half of the vector instead of the last half? Deleting the last half will be much more efficient.
And don't forget about he affect erase has on iterators (invalidation).