声明后在C ++向量中分配元素 [英] Allocating elements in C++ vector after declaration
本文介绍了声明后在C ++向量中分配元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参考下面的代码和注释:
Please refer to the code and comments below:
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.
推荐答案
您只需要在添加新值之前重新调整向量大小即可:
You simply need to resize the vector before adding the new values:
v1.resize(20);
这篇关于声明后在C ++向量中分配元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文