Limiting number of elements in vector

吃可爱长大的小学妹 提交于 2019-12-02 11:06:44

You use cin once before the loop, and once inside the loop that is repeated 10 times. 1 + 10 equals 11 and therefore the input is asked for 11 times. To limit the number of inputs taken to 10, you need to limit the calls to cin to 10.

Because when you record type your 10th elements, vector still have 9 elements. So on the next loop turn, you'll add the 10th in vector and ask for the 11th.

If you know it will be 10 elements to input, why not using C++11 std::array ?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!