Bloomberg LP Interview Question
Software Engineer / DevelopersIF we are decreasing the size of vector, or if we are increasing the size but the vector still has enough capacity, there's no need to allocate memory. When capacity is not enough, the function will cause a reallocation: 1). allocate contiguous memory elsewhere, which can hold the original vector plus new elements. if such a space is not available, return false; 2) copy the original vector and then reclaim the space; 3) add new elements at the back.
sorry, when reallocation is not successful, this function will not return a false, it's a void function. Instead, a bad_alloc exception will be thrown.
vivi,
so do you mean if contigeous memory is not available then vector resize function throw errors ?
here is the declaration of resize:
void resize(size_type sz, T c = T());
if sz < vector's size then resize should have this effect:
erase(begin()+sz,end());
if sz > vector's size then resize should have this effect:
insert(end(), sz-size(), c);
if the size is equal, then nothing is done.
Note that I am not telling that your implementation must call either erase or insert, but it must behave like...
if we are decreasing the size of vector then there is no need to reallocate the vector.
- tito March 31, 2010If we are increasing the size of vector and there is no contiguous memory then vector won't get allocated ... it will return error.....