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

前端 未结 6 2379
感动是毒
感动是毒 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 02:49

    at() throws an out_of_range exception, which [] doesn't do.

    So while [] might make your application crash immediately if you try to access something out of range, at() will enable to you to handle the error at runtime.

    If this is necessary for you (often times it won't be because accessing something out of range automatically means that the semantics of your code don't work as they're supposed to) you should use at().

提交回复
热议问题