码迷,mamicode.com
首页 > 其他好文 > 详细

vector shrink_to_fit

时间:2014-07-10 21:17:43      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:vector   size   capacity   


#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int>vec;
    for(int i = 0 ;i  < 100 ; ++i)
        vec.push_back(i);
    cout << vec.size() << endl; //100
    cout << vec.capacity() << endl; //128
    vec.erase(vec.begin()+10,vec.end()); //改变了size,但是并未改变capccity
    cout << vec.size() << endl; //10
    cout << vec.capacity() << endl; //128
    vector<int>(vec).swap(vec);
    cout << vec.size() << endl; //10
    cout << vec.capacity() << endl; //10
    vec.clear(); //clear并未真正释放空间!!!
    cout << vec.size() << endl; //0
    cout << vec.capacity() << endl; //10
    vector<int> (vec).swap(vec); //这才真正释放了空间!!
    cout << vec.size() << endl; //0
    cout << vec.capacity() << endl; //0
    return 0;
}


PS:C++11中已经实现了shink_to_fit函数。实现上述功能。



vector shrink_to_fit,布布扣,bubuko.com

vector shrink_to_fit

标签:vector   size   capacity   

原文地址:http://blog.csdn.net/xuqingict/article/details/37603913

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!