我在学校里学习了string::at
,但通过探索字符串库,我看到了string::operator[]
,这是我之前从未见过的。
现在我正在使用operator[]
,并且自从那时以来就没有使用过at
,但两者有什么区别呢?
以下是一些示例代码:
std::string foo = "my redundant string has some text";
std::cout << foo[5];
std::cout << foo.at(5);
就输出而言,它们基本相同,但是否存在我未意识到的一些微小差异?
.at()
切换到operator[]
? - us2012