原文
myvector.resize(n);
resize可以调整vector成n个元素
myvector.resize(n, val);
还可以带参数 val 这样如果当前vector的size < n,多出来的就会用 val填充
// resizing vector
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
// set some initial content:
for (int i = 1; i < 10; i++) myvector.push_back(i);
// myvector = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
myvector.resize(5);
// myvector = {1, 2, 3, 4, 5}
myvector.resize(8, 100);
// myvector = {1, 2, 3, 4, 5, 100, 100, 100}
myvector.resize(12);
std::cout << "myvector contains:";
for (int i = 0; i < myvector.size(); i++)
cout << ' ' << myvector[i];
cout << '\n';
return 0;
}