例子
#include <iostream>
#include<vector>
#include<algorithm>
#include "CPPDemo.h"
#include<iomanip>
#include<set>
#include<vector>
using namespace std;
int main() {
vector<int> num;
int i = 1;
unsigned int capacity = num.capacity();
for (int temp = 0; temp < 1000; temp++)
{
num.push_back(1);
//容量发生改变时输出
if (num.capacity() != capacity)
{
capacity = num.capacity();
cout << num.capacity() << endl;
}
}
return 0;
}
输出结果:
可以看出,每次增长的时候都是原来的容量×1.5倍=新容量
IDE:VS2017