关于vector已经写的差不多了,似乎要接近尾声了,从初始化到如何添加元素再到copy元素都有所涉及,是时候谈一谈内存的释放了。

是的,对于数据量很小的vector,完全没必要自己进行主动的释放,因为那样对程序的效率几乎没有影响。但是当vector中存入大量的数据后,并且都数据进行了一些操作,比如删除后,如果我们能积极主动的去释放内存,那么是非常明智的。

写到这里,应该明确了size和capacity的区别了。

现在介绍一个方法,std::vector::clear()
Removes all elements from the vector (which are destroyed), leaving the container with a size of .
看清楚了吗,英文中提到的是size=,而非capacity。写程序验证一些: #include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back(); cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl; v.clear();
cout << "after clear size:" << v.size() << endl;
cout << "after clear capacity:" << v.capacity() << endl;
return ;
}
//输出
size:
capacity:
after clear size:
after clear capacity: 看到了吗,clear后,size变为了0,capacity没有变化。再读一读clear的英文描述:
A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap: vector().swap(x); // clear x reallocating 所以这个时候swap该出厂了。 std::vector::swap
Exchanges the content of the container by the content of x, which is another vector object of the same type. Sizes may differ. After the call to this member function, the elements in this container are those which were in x before the call, and the elements of x are those which were in this. All iterators, references and pointers remain valid for the swapped objects. Notice that a non-member function exists with the same name, swap, overloading that algorithm with an optimization that behaves like this member function. 直接看看使用: #include <iostream>
#include <vector> int main()
{
std::vector<int> foo;
foo.push_back();
foo.push_back();
foo.push_back();
foo.push_back();
foo.push_back(); std::vector<int> bar;
bar.push_back();
bar.push_back(); std::cout << "foo size:" << foo.size() << std::endl;
std::cout << "foo capacity:" << foo.capacity() << std::endl; std::cout << "bar size:" << bar.size() << std::endl;
std::cout << "bar capacity:" << bar.capacity() << std::endl;
foo.swap(bar); std::cout << "after swap foo size:" << foo.size() << std::endl;
std::cout << "after swap foo capacity:" << foo.capacity() << std::endl; std::cout << "after swap bar size:" << bar.size() << std::endl;
std::cout << "after swap bar capacity:" << bar.capacity() << std::endl; return ;
}
//输出:
foo size:
foo capacity:
bar size:
bar capacity:
after swap foo size:
after swap foo capacity:
after swap bar size:
after swap bar capacity:
看到了吗,swap之后,不仅仅是size变化了,capacity也是变化了。那么于是就把swap替代clear了: #include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back(); cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl; vector<int>().swap(v);
cout << "after swap size:" << v.size() << endl;
cout << "after swap capacity:" << v.capacity() << endl;
return ;
}
//输出:
size:
capacity:
after swap size:
after swap capacity:
还记得上篇博客的shrink_to_fit()吗,如果clear后在调用shrink_to_fit()不一样可以吗? #include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back(); cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl; v.clear();
v.shrink_to_fit();
cout << "after swap size:" << v.size() << endl;
cout << "after swap capacity:" << v.capacity() << endl;
return ;
}
//输出:
size:
capacity:
after swap size:
after swap capacity:
所以 不用以为只有swap替代clear才能正确释放vector的内存,C++11推出了shrink_to_fit方法,也可以达到目的。

实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())的更多相关文章

  1. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  2. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  3. 正确释放Vector的内存

    http://blog.jobbole.com/37700/ 今天在看微博的时候, 有人提出了一个对于Vector内存泄露的疑问( Link). 博主采用 Vector存储一些数据,但是发现在执行 c ...

  4. 实战c++中的string系列--std::string与MFC中CString的转换

    搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...

  5. 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)

    今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...

  6. 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())

    在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...

  7. 实战c++中的string系列--不要使用memset初始化string(一定别这么干)

    參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ...

  8. 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)

    非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ...

  9. 实战c++中的string系列--指定浮点数有效数字并转为string

    上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...

随机推荐

  1. 关于QObject类的一些理解

    QRunnable并没有继承自QObject,所以它和其他QObject组件的通信不能使用传统的信号和槽,要是用信号和槽我们必须将其继承自QObject自动的添加 QThread的退出最好用exit( ...

  2. Git 学习笔记(一)

    某大牛曾经说过,版本控制的最大好处就是让你可以永远后悔,而 Git 无疑是众多版本控制软件当中的佼佼者,在开源社区更是备受青睐,那么它为何会诞生,和其他的版本控制软件项目又有什么不同?且让我们慢慢来看 ...

  3. 高并发之后端优化(PHP)

    页面静态化 使用模板引擎 可以使用Smarty的缓存机制生成静态HTML缓存文件 $smarty->cachedir=$ROOT·"/cache"://缓存目录 $smart ...

  4. python对象 -- 组合

    详解组合:#Demo1class Game_kind: def __init__(self,nickname,sex,hp,ad): self.nickname = nickname self.sex ...

  5. ThinkPHP5(目录,路径,模式设置,命名空间)

    ThinkPHP是一个快速.兼容而且简单的轻量级国产PHP开发框架 目录结构 路径: http://www.tp5.comm/index.php/admin/index/index入口文件    pu ...

  6. P2740 [USACO4.2]草地排水Drainage Ditches

    题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没 ...

  7. css元素垂直居中方法

    1.Line-height 适用情景:单行文字垂直居中技巧 这个方式应该是最多人知道的了,常见于单行文字的应用,像是按钮这一类对象,或者是下拉框.导航此类元素最常见到的方式了.此方式的原理是在于将单行 ...

  8. Golden Gate 检查点

    检查点是记录读写位置信息,在恢复时候要用到,保证事务的完整性. 两种存储方式: 存放在dirchk下 存放在指定的checkpoint table Replicat: nodbcheckpoint: ...

  9. Apache2.2伪静态配置

    最近由于工作的需要要配置一下Apache的伪静态化,在网上搜了好多都无法完成,所以觉得有必要在这里写一下. 第一步:打开Apache的httpd.conf文件,把LoadModule rewrite_ ...

  10. ZBrush中如何做不同图案的遮罩

    ZBrush®软件中不仅可以创建矩形遮罩还可以创建有图案的遮罩,且是非常简单有效的,那么究竟怎样做出神奇的效果,本文将为您详细讲解. 有关反转遮罩.清除遮罩的详细内容,请点击:ZBrush中如何反选遮 ...