vector利用swap()函数进行内存的释放 和capacity()、size()、clear()、reserve()函数

来源:互联网 发布:网络伤感情歌大全100首 编辑:程序博客网 时间:2024/06/06 15:51

首先,vector与deque不同,其内存占用空间只会增长,不会减小。比如你首先分配了10,000个字节,然后erase掉后面9,999个,则虽然有效元素只有一个,但是内存占用仍为10,000个。所有空间在vector析构时回收。

1、释放内存:
   empty()是用来检测容器是否为空的,clear()可以清空所有元素。但是即使clear(),所占用的内存空间依然如故。如果你需要空间动态缩小,可以考虑使用deque。如果非要用vector,这里有一个办法:

    在《effective STL》和其他很多C++文章中都有指明,用clear()无法保证内存回收。但是swap技法可以。具体方法如下所示:
    vector<int> ivec;
    ivec.push_back(1);ivec.push_back(1);ivec.push_back(2);ivec.push_back(2);
    vector<int>().swap(ivec); //或者ivec.swap(vector<int>());

    vector<int>().swap(ivec); 或者如下所示 加一对大括号都可以,意思一样的:
    {
     std::vector<int> tmp;   
     ivec.swap(tmp);
    }     
    加一对大括号是可以让tmp退出{}的时候自动析构

2、修整空间
在一个应用中,可能会需要向一个vector中插入很多记录,比如说100000条,为了避免在插入过程中移动内存,咱实现向系统预订一段足够的连续的空间,例如
vector<int> ivec;
ivec.reserve(100000);
这个问题是解决了。
但是,如果后来这个vector不再需要存那么多的元素了,已经通过erase删除了。但是以前咱们预留的空间却无法被其他程序再度利用,这样会造成内存一定程度上的浪费。于是,我们利用目前的vector构造一个一模一样的vector,他并没有预留空间,于是以前预留的空间也被释放以作他用了:
ivec.swap(vector<int>(ivec)); // or vector<int>(ivec).swap(ivec)
或者如下所示 加一对大括号都可以,意思一样的:
    {
     std::vector<int> tmp = ivec;   
     ivec.swap(tmp);
    }     
    加一对大括号是可以让tmp退出{}的时候自动析构

     使用这种方法的前提是vector从前存储了大量数据,比如10000000,经过各种处理后,现在只有100条,那么向清空原来数据所占有的空间,就可以通过这种交换技术swap技法就是通过交换函数swap(),使得vector离开其自身的作用域,从而强制释放vector所占的内存空间。


#include<iostream>#include<vector>using namespace std;int main(){    vector <int> v1, v2, v3;    cout << "The number of elements in v1: " << v1.size()<<"  the capacity of v1:"<< v1.capacity() << endl;    cout << "The number of elements in v2: " << v2.size()<<"  the capacity of v2:"<< v2.capacity() << endl;    v1.push_back( 1 );    v1.push_back( 2 );    v1.push_back( 3 );    v2.push_back( 10 );    v2.push_back( 20 );    cout<<endl;    cout<<"after push 3 elements into v1  and 2 elements into v2"<<endl;    cout<<endl;    cout << "The number of elements in v1: " << v1.size()<<"  the capacity of v1:"<< v1.capacity() << endl;    cout << "The number of elements in v2: " << v2.size()<<"  the capacity of v2:"<< v2.capacity() << endl;    cout<<endl;    cout<<"after swap v1 and v2"<<endl;    cout<<endl;    v1.swap( v2 );    cout << "The number of elements in v1: " << v1.size()<<"  the capacity of v1:"<< v1.capacity() << endl;    cout << "The number of elements in v2: " << v2.size()<<"  the capacity of v2:"<< v2.capacity() << endl;    v1.swap(v2);    cout<<endl;    cout<<"swap bcak of v1 and v2"<<endl;    cout<<"after swap v1 and v3"<<endl;    cout<<endl;    v1.swap(v3);    cout << "The number of elements in v1: " << v1.size()<<"  the capacity of v1:"<< v1.capacity() << endl;    cout << "The number of elements in v3: " << v3.size()<<"  the capacity of v3:"<< v3.capacity() << endl;    cout<<endl;    v2.clear();    cout<<"after execute v2.clear()"<<endl;    cout << "The number of elements in v2: " << v2.size()<<"  the capacity of v2:"<< v2.capacity() << endl;    vector<int>().swap(v2);    cout << "The number of elements in v2: " << v2.size()<<"  the capacity of v2:"<< v2.capacity() << endl;    return 0;}



0 0
原创粉丝点击