Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What if size argument for std::vector::resize is equal to the current size?

Reading the manual about vector::resize http://www.cplusplus.com/reference/vector/vector/resize/

It only says what happens if the size is greater or smaller, but does not say what happens if it's equal. Is it guaranteed that on equal size it will not reallocate the array and invalidate the iterators?

I wanted to avoid one branch and handle only 2 cases (>= or <) instead of 3 (< or > or ==), but if resizing to same size is undefined, then i will have to check that case too.

like image 707
Youda008 Avatar asked Oct 29 '25 00:10

Youda008


1 Answers

This is probably just an error in the linked reference. The standard says following:

void resize(size_type sz);

Effects: If sz < size(), erases the last size() - sz elements from the sequence. Otherwise, appends sz - size() default-inserted elements to the sequence.

Since sz - size() is 0 in your case, it doesn't do anything.

like image 85
Zereges Avatar answered Oct 30 '25 15:10

Zereges