實戰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 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:5
capacity:6
after clear size:0
after clear capacity:6

看到了嗎,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:5
foo capacity:6
bar size:2
bar capacity:2
after swap foo size:2
after swap foo capacity:2
after swap bar size:5
after swap bar capacity:6

看到了嗎,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:5
capacity:6
after swap size:0
after swap capacity:0

還記得上篇博客的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:5
capacity:6
after swap size:0
after swap capacity:0

所以 不用以爲只有swap替代clear才能正確釋放vector的內存,C++11推出了shrink_to_fit方法,也可以達到目的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章