首页 > 其他 > 详细

vector shrink_to_fit

时间:2014-07-10 21:18:00      阅读:321      评论:0      收藏:0      [点我收藏+]


#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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!