purpose of capacity()having resize()

This is a discussion on purpose of capacity()having resize() within the C++ Programming forums, part of the General Programming Boards category; Hy all;;;
Being novice to using vectors in Cpp ...I would like to know in more depth the purpose of ...

purpose of capacity()having resize()

Hy all;;;
Being novice to using vectors in Cpp ...I would like to know in more depth the purpose of capacity() albeit we have resize() available...i mean that if i can resize the size i initially assigned to a vector than why do i need capacity or why this function is useful and when?(studied that capacity gives on hood memory which limits the size() function usage within available capacity)

You might use the capacity() member function to determine if pushing back the next few elements would cause a re-allocation, hence invalidating say, iterators to your elements at a most inconvenient moment. If so, you could then reserve() the desired capacity, and then get your iterators, and then push back the elements. You might prefer this to actually resizing since the new size would cause new elements to be created immediately, when instead you want to push back or otherwise insert the elements.

You might use the capacity() member function to determine if pushing back the next few elements would cause a re-allocation, hence invalidating say, iterators to your elements at a most inconvenient moment. If so, you could then reserve() the desired capacity, and then get your iterators, and then push back the elements. You might prefer this to actually resizing since the new size would cause new elements to be created immediately, when instead you want to push back or otherwise insert the elements.

Well for one it's a good way to check how much space you have left before the next resize will be needed upon a push_back.

One might, for example, use a vector to store particles from effects in a 3D game, and upon size reaching capacity, avoid creating more particles and instead note down that the vector will need growing after the current frame is rendered.

Well for one it's a good way to check how much space you have left before the next resize will be needed upon a push_back.

One might, for example, use a vector to store particles from effects in a 3D game, and upon size reaching capacity, avoid creating more particles and instead note down that the vector will need growing after the current frame is rendered.