请参考下面的代码和注释:
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<int> v1(10);
实际上与vector<int> v1; v1.resize(20);
是相同的。话虽如此,你对push_back
有什么意见(或者说,你对reserve
后跟着push_back
有什么意见)? - James McNellis