What is the difference between std::vector::reserve() and std::vector::resize()?
Last updated
reserve(n)only changes capacity (allocated memory).size()is unchanged and no elements exist, so accessing them is undefined behavior.resize(n)sets size to exactly n, constructing value-initialized elements when growing or destroying trailing ones when shrinking.Both may reallocate and invalidate iterators if capacity must grow.
- https://stackoverflow.com/questions/7397768/choice-between-vectorresize-and-vectorreserve
- https://www.geeksforgeeks.org/cpp/increase-the-capacity-of-vector-in-cpp/
- https://stackoverflow.com/questions/60583054/why-is-resizing-a-vector-faster-than-reserve-and-push-back
- https://www.geeksforgeeks.org/cpp/resize-vector-without-initializing-new-elements-in-cpp/
- https://www.geeksforgeeks.org/cpp/vector-resize-c-stl/