实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())
关于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())的更多相关文章
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ...
- 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)
之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...
- 正确释放Vector的内存
http://blog.jobbole.com/37700/ 今天在看微博的时候, 有人提出了一个对于Vector内存泄露的疑问( Link). 博主采用 Vector存储一些数据,但是发现在执行 c ...
- 实战c++中的string系列--std::string与MFC中CString的转换
搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...
- 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)
今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...
- 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())
在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...
- 实战c++中的string系列--不要使用memset初始化string(一定别这么干)
參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ...
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)
非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ...
- 实战c++中的string系列--指定浮点数有效数字并转为string
上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...
随机推荐
- 【NOI 2015】 程序自动分析
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4195 [算法] 并查集 [代码] #include<bits/stdc++.h ...
- ubuntu 使用阿里云 apt 源
以下内容来自 https://opsx.alibaba.com/mirror Ubuntu对应的“帮助”信息 修改方式:打开 /et/apt/sources.list 将http://archive. ...
- ubuntu中不能远程连接解决
今天装好ubuntu19.04之后不能远程连接,网上找了很久终于自己解决了.ap 步骤如下:希望对各位有用,哪里不对请指出 第一步我们需要加载openssh-server 等待加载完毕后, ...
- Ubuntu下推荐安装软件
前言:都是全平台软件,通用性好. 1.搜狗输入法 官网下载: 不能双击.deb安装成功,需要安装依赖,可参考:https://www.cnblogs.com/chendeqiang/p/1017741 ...
- User_Login_Register_Shopping+装饰器 3.0
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018/5/27 0027 14:07# @Author : Anthony.Waa# @ ...
- C#操作IIS服务
进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理. [1]前提掌握要点: (1).IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0, ...
- Codeforces Round #284 (Div. 2) A
解题思路:给出 n个电影的精彩时段(a[i],b[i]),和每次可以跳过的时间x,问要看完所有的精彩时刻,至少需要看多长时间的电影. 因为要时间最少,所有除了精彩时刻的电影则能跳过就跳过(用取余来算) ...
- JS 数值转换、加减乘除
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- day09-3 数据类型总结,深浅拷贝
目录 数据类型总结,深浅拷贝 存一个值还是多个值 有序 or 无序 可变 or 不可变 浅拷贝和深拷贝的区别(只针对可变类型) 1.拷贝: 3.深拷贝 总结: 数据类型总结,深浅拷贝 存一个值还是多个 ...
- -ms-,-moz-,-webkit-,-o-含义及各浏览器内核整理
transform:rotate(30deg); //统一标识语句 -ms-transform:rotate(30deg); //-ms代表ie内核识别码 ...