I have a vector of events, with the first one being a guard for easier computations. I have implemented a reset operation as follows:
void reset()
{
myVector.resize(1);
}
so that only the first one element remains. Does this operation reallocate memory? I will call it in a loop, so I would like it to be very time-efficient. If it does, then what would be an efficient alternative?
If there are no elements in the vector, then the first call to
resize(1);will do the allocation. Otherwise, no.The efficient alternative is to assign the vector with enough space during it’s construction. Then you are sure no reallocation is going to happen.