当前位置: 代码迷 >> 综合 >> 实战c++中的vector系列--正确释放vector的内存 clear swap shrink to fi
  详细解决方案

实战c++中的vector系列--正确释放vector的内存 clear swap shrink to fi

热度:61   发布时间:2024-01-21 07:49:02.0
       

关于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 0.
看清楚了吗,英文中提到的是size=0,而非capacity。写程序验证一些:

#include<iostream>#include<vector>using namespace std;int main(){    vector<int> v;    v.push_back(1);    v.push_back(2);    v.push_back(3);    v.push_back(4);    v.push_back(5);    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 0;}//输出size:5capacity:6after clear size:0after clear capacity:6
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

看到了吗,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(1);    foo.push_back(2);    foo.push_back(3);    foo.push_back(4);    foo.push_back(5);    std::vector<int> bar;      bar.push_back(1);    bar.push_back(2);    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 0;}//输出:foo size:5foo capacity:6bar size:2bar capacity:2after swap foo size:2after swap foo capacity:2after swap bar size:5after swap bar capacity:6
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

看到了吗,swap之后,不仅仅是size变化了,capacity也是变化了。那么于是就把swap替代clear了:

#include<iostream>#include<vector>using namespace std;int main(){    vector<int> v;    v.push_back(1);    v.push_back(2);    v.push_back(3);    v.push_back(4);    v.push_back(5);    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 0;}//输出:size:5capacity:6after swap size:0after swap capacity:0
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

还记得上篇博客的shrink_to_fit()吗,如果clear后在调用shrink_to_fit()不一样可以吗?

#include<iostream>#include<vector>using namespace std;int main(){    vector<int> v;    v.push_back(1);    v.push_back(2);    v.push_back(3);    v.push_back(4);    v.push_back(5);    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 0;}//输出:size:5capacity:6after swap size:0after swap capacity:0
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

所以 不用以为只有swap替代clear才能正确释放vector的内存,C++11推出了shrink_to_fit方法,也可以达到目的。

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  相关解决方案