C++ vector动态容量变化

Tips:也可以尝试对deque,list进行类似的实验以加深理解。

#include <iostream>
#include <vector>
//#include <deque>
using namespace std;

int main()
{
    vector <int> a;
    for(int i =0;i < 10;i++)
    {
        a.push_back(i);
        cout << "增加一个元素后,vector的容量为:" << a.capacity() << endl;
    }
    for(int j = 0; j < 10;j++)
    {
        cout << a[j] << " ";
    }
    return 0;
}

结果如图:

image

原文地址:https://www.cnblogs.com/Jesse-Cavendish/p/14528317.html