例子

#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;
}

 输出结果:

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

 

可以看出,每次增长的时候都是原来的容量×1.5倍=新容量

 

IDE:VS2017

相关文章:

  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2021-04-30
  • 2021-12-22
猜你喜欢
  • 2022-01-21
  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案