Loading

[C++]vector内存的增长机制

例子

#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

posted @ 2019-03-02 19:15  李正浩  阅读(652)  评论(0编辑  收藏  举报