Should I use std::vector::at() in my code

前端 未结 6 2383
感动是毒
感动是毒 2021-02-19 02:17

I noticed today that std::vector::at() is significantly slower than accessing values with square brackets []. According to the doc .at() i

6条回答
  •  一个人的身影
    2021-02-19 03:01

    at does range check, but operator[] does not. For example, if you pass -1 to at(), an std::out_of_range will be thrown. But if you do the same thing to operator[] it will crash or strange things will happen.

    If you are absolutely sure that the index is OK or you want to do the check yourself, use operator[].

提交回复
热议问题