请引用下面的代码和注释:
vector<int> v1(10);
cin>>v1[0]; // allowed
cin>>v1[1]; // allowed
// now I want v1 to hold 20 elements so the following is possible:
cin>>v1[15]>>v[19]; // how to resize the v1 so index 10 to 19 is available.
最佳答案
您只需在添加新值之前调整 vector 的大小:
v1.resize(20);
关于c++ - 声明后在 C++ vector 中分配元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5922797/