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

前端 未结 6 1546
灰色年华
灰色年华 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:55

    The only way to really get rid off unused memory in a std::vector<> pre C++11 is to swap it with an empty vector: vector().swap(myvec). In C++11 you have a member function shrink_to_fit which often is implemented as the swap idiom just mentioned.

提交回复
热议问题