C++: Vector bounds

后端 未结 4 1512
北海茫月
北海茫月 2020-12-11 01:40

I am coming from Java and learning C++ in the moment. I am using Stroustrup\'s Progamming Principles and Practice of Using C++. I am working with vectors now. On page 117 he

4条回答
  •  情歌与酒
    2020-12-11 01:57

    C and C++ does not always do bounds checks. It MAY cause a runtime error. And if you were to overdo your number by enough, say 10000 or so, it's almost certain to cause a problem.

    You can also use vector.at(10), which definitely should give you an exception. see: http://www.cplusplus.com/reference/vector/vector/at/ compared with: http://www.cplusplus.com/reference/vector/vector/operator%5B%5D/

提交回复
热议问题