如何正确解除分配或删除一个c ++向量? [英] How to correctly deallocate or delete a c++ vector?

查看:221
本文介绍了如何正确解除分配或删除一个c ++向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题与向量在C + +。

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

我创建了一个向量并插入10000整数值,并已检查内存利用率。它是600 kb。但是我擦除了矢量,仍然是我的系统监视器说程序使用600 kb。

I created a vector and inserted 10000 integer values into it and have checked the memory utilization. It is 600 kb. But after i erased the vector, still my system monitor says the program uses 600 kb.

任何人都可以解释为什么内存不会被释放,即使我擦除矢量

Can anyone explain why the memory is not getting freed even after i erase the vector

注意:我已经使用所有的方法删除(擦除,pop_front,pop_back,清除...即使我有同样的问题)

Note: I have used all the methods for deletion(erase,pop_front,pop_back,clear...Even then I have the same problem)

感谢和问候...:)

推荐答案

std :: vector<> pre C ++ 11中的内存是用空向量交换它: vector< int>()。 swap(myvec)。在C ++ 11中,你有一个成员函数 shrink_to_fit ,通常被实现为刚才提到的交换惯用法。

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<int>().swap(myvec). In C++11 you have a member function shrink_to_fit which often is implemented as the swap idiom just mentioned.

这篇关于如何正确解除分配或删除一个c ++向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆