gpt4 book ai didi

c++ - 当字符串下标超出范围时,不抛出异常

转载 作者:太空宇宙 更新时间:2023-11-04 16:08:12 25 4
gpt4 key购买 nike

此代码不打印任何内容,但也不会产生错误。为什么?

string s = "test";
cout << s[5];

最佳答案

std::stringoperator[]documented to not do any bounds checking :

Returns a reference to the character at specified location pos. No bounds checking is performed.

要使用边界检查从字符串中读取,请使用 at() :

Returns a reference to the character at specified location pos. Bounds checking is performed, exception of type std::out_of_range will be thrown on invalid access.

关于c++ - 当字符串下标超出范围时,不抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31910794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com