Shrink to fit reallocation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Shrink to fit reallocation

Hi Please refer code below: I know shrink to fit is implementation specific and may or may not re allocate the data again? Question is about the way to avoid the reallocation. Can move help or re allocation is non avoidable in any means ? https://sololearn.com/compiler-playground/cJT2gcb8CMAj/?ref=app

5th Jan 2024, 4:06 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 1
from google: "The usual way to avoid vector reallocation is to to use the reserve() function to reserve, up front, enough memory for a given number of elements." so... create a large enough vector capacity. try not to exceed it. avoid using shrink_to_fit.
5th Jan 2024, 10:58 PM
Bob_Li
Bob_Li - avatar
+ 1
That's true . Just curious whether is it possible to avoid re allocation if shrink to fit was used.
8th Jan 2024, 2:24 AM
Ketan Lalcheta
Ketan Lalcheta - avatar