Why is using “vector.at(x)” better than “vector[x]” in C++?

前端 未结 4 1466
说谎
说谎 2020-12-09 15:20

If I want to get to a value in vector I can use two options : use the [] operator. Or I might use the function .at example for using :

vector ive         


        
4条回答
  •  孤街浪徒
    2020-12-09 15:56

    The other answers are entirely correct, but may give the impression that it is a good idea to use at() when writing code that you'll still be debugging, as that will cause an error to be signalled. This is the case in general, but doesn't apply for most widespread compilers -- gcc can be set to perform (fatal) range checks by putting it in debug mode and something similar can probably be done in Visual Studio.

提交回复
热议问题