How to correctly deallocate or delete a c++ vector?

前端 未结 6 1674
灰色年华
灰色年华 2021-01-25 11:00

I have a weird problem with vector in C++..

I created a vector and inserted 10000 integer values into it and have checked the memory utilization. It is 600 kb. But after

6条回答
  •  醉酒成梦
    2021-01-25 11:48

    The C++ vector reserves more memory than it needs for its elements to speed up adding new elements and it doesn't free the reserved memory, after the elements have been deleted. You can try swapping the vector with itself, to make the amount of reserved memory match the actual size of all the elements: v.swap(v)

提交回复
热议问题