What is the difference between string::at and string::operator[]?

后端 未结 3 1729
無奈伤痛
無奈伤痛 2020-12-07 01:01

I was taught string::at in school, but by exploring the string library I saw string::operator[], which I was never shown before.

I\'m now u

3条回答
  •  隐瞒了意图╮
    2020-12-07 01:22

    Yes, there is one major difference: using .at() does a range check on index passed and throws an exception if it's over the end of the string while operator[] just brings undefined behavior in that situation.

提交回复
热议问题