关于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. js最简单的-点击小图放大

    js最简单的-点击小图放大 标签(空格分隔): js <html> <body> <img class="imgview" src="{$v ...

  2. oracle (9I/10G/11G)数据库日志挖掘(审计误操作)

    文档结构: 资料来自官方网站: https://docs.oracle.com/cd/E11882_01/server.112/e22490/logminer.htm#SUTIL019 来自论坛: h ...

  3. python一行代码实现9x9乘法表

    for i in range(1,10): for j in range(1,i+1): print('%s * %s = %s ' %(i,j,i*j),end="") prin ...

  4. caffe study- AlexNet 之算法篇

    在机器学习中,我们通常要考虑的一个问题是如何的“以偏概全”,也就是以有限的样本或者结构去尽可能的逼近全局的分布.这就要在样本以及结构模型上下一些工夫. 在一般的训练任务中,考虑的关键问题之一就是数据分 ...

  5. nginx令牌限制并发

    http{ limit_req_zone $binary_remote_addr zone=req_one:10m rate=100r/s; server { listen 8080; server_ ...

  6. 关于TCP的三次握手和四次分手 专题

    客户端TCP状态迁移:CLOSED->SYN_SENT->ESTABLISHED->FIN_WAIT_1->FIN_WAIT_2->TIME_WAIT->CLOSE ...

  7. 后端向服务器发送客户端请求--HttpWebRequest

    HttpWebRequest类与HttpRequest类的区别 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息. HttpWebReques ...

  8. php基础------将二维数组转三维数组

    将二维数组转为三维数组 /** * 二维数组转三维数组(指定键为三维数组的键名) * @param [type] $arr [要排序的数组] * @param [type] $key [指定的键] * ...

  9. Java校验8位字符串是否为正确的日期格式

    import java.text.ParseException; import java.text.SimpleDateFormat; /** * 校验8位字符串是否为正确的日期格式 * @autho ...

  10. BZOJ2440: [中山市选2011]完全平方数 容斥原理_莫比乌斯函数

    emmm....... 数学题都不友好QAQ...... Code: #include <cstdio> #include <algorithm> #include <c ...