如果n小于当前容器的大小,则内容将减少到其前n个元素,并将其删除(并销毁)。
如果n大于当前容器的大小,则通过在末尾插入尽可能多的元素来扩展内容,以达到n的大小。如果指定了val,则将新元素初始化为val的副本,否则,将对它们进行值初始化。
//---------------
std::vector<int> check;
for (int i = 0; i < 10; i++) { check.push_back(i); }
for (int i = 0; i < check.size(); i++) { std::cout << check[i] <<",,,"; }
std::cout << endl;
check.resize(5);
for (int i = 0; i < check.size(); i++) { std::cout << check[i] << ",,,"; }
std::cout << endl;
check.resize(10);
for (int i = 0; i < check.size(); i++) { std::cout << check[i] << ",,,"; }
check.resize(15, 99);
for (int i = 0; i < check.size(); i++) { std::cout << check[i] << ",,,"; }
通过前后变化的关系可以看出规律