Sha*_*fiz 5 c++ resize vector dynamic
请参阅下面的代码和评论:
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.
Run Code Online (Sandbox Code Playgroud)
您只需在添加新值之前调整向量的大小:
v1.resize(20);
Run Code Online (Sandbox Code Playgroud)