关于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. java多线程 interrupt(), interrupted(), isInterrupted()方法区别

    interrupt()方法: 作用是中断线程. 本线程中断自身是被允许的,且"中断标记"设置为true 其它线程调用本线程的interrupt()方法时,会通过checkAcces ...

  2. 为IT程序员量身定制的12个目标——很经典

    对程序员们来说挑战自我非常重要,要么不断创新,要么技术停滞不前.新年伊始,我整理了12个月的目标,每个目标都是对技术或个人能力的挑战,而且可以年复一年循环使用. 01. 变得有耐心 02. 保持健康 ...

  3. Maven 学习笔记(三)

    有时我们在项目中可能需要打包一个可执行的 jar 包,我最近也遇见了,很傻很天真的用了如下配置: <packaging>jar</packaging> 效果一如既往的好,打包成 ...

  4. 在Android源码下编译jni所需要知道的事~

    以下只是自己的一些总结,欢迎讨论 通过NDK编译jni网上有很多例子,在这我只总结在Android源码下编译 1.android源码环境下编译so包,编出来的.so的包前面不会自动给添加lib,NDK ...

  5. windows中安装redis的phpredis扩展

    1. 下载php的redis扩展 打开网址 http://pecl.php.net/ (php的扩展库官网),搜索redis,进入地址:http://pecl.php.net/package/redi ...

  6. Android Studio配置GreenDAO 3.2.0和使用方法

    我相信,在平时的开发过程中,大家一定会或多或少地接触到SQLite.然而在使用它时,我们往往需要做许多额外的工作,像编写SQL语句与解析查询结果等.所以,适用于Android ORM框架也就孕育而生了 ...

  7. Android跳转到系统Wifi界面的方式

    第一种 Intent intent = new Intent(); intent.setAction("android.net.wifi.PICK_WIFI_NETWORK"); ...

  8. 。net内存优化

    1.尽量减少和避免不必要的对象 2.优化算法和数据结构 3.采用非托管代码或者模块编写数据处理逻辑 4.NET应用程序的内存一定程度上受垃圾回收的影响.并指出,一些数据结构如List,系统会分配多余的 ...

  9. activity_note

    在activiti任务中,主要分为两大类查询任务(个人任务和组任务): 1.确切指定了办理者的任务,这个任务将成为指定者的私有任务,即个人任务. 2.无法指定具体的某一个人来办理的任务,可以把任务分配 ...

  10. List of features and minimum Clang version with support

    #1: C++11 Language Feature C++11 Proposal Available in Clang? Rvalue references N2118 Clang 2.9      ...