qfedu-cpp-level/day9/stl_vector_demo/d3.cpp

29 lines
757 B
C++
Raw Normal View History

// vector 的使用
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v1;
// int mSize = v1.size(); // 获取容器中元素的个数
int mCapacity = v1.capacity(); // 获取容器的容量
int cnt = 0; // 扩容次数
cout << "初始容量: " << mCapacity << endl;
for (int i = 0; i < 1000; i++)
{
v1.push_back(i); // 向容器中添加元素
// cout << "size = " << v1.size() << " capacity = " << v1.capacity() << endl;
if (mCapacity != v1.capacity())
{
mCapacity = v1.capacity();
cout << "" << ++cnt << "次扩容, "
<< "当前容量为: " << v1.capacity() << endl;
}
}
return 0;
}