gpt4 book ai didi

c++ - 调用空字符串的 c_str

转载 作者:可可西里 更新时间:2023-11-01 17:23:31 28 4
gpt4 key购买 nike

这个代码片段是正确的还是会导致未定义的行为?

std::string s;
assert(strlen(s.c_str())==0);

如果不是undefined behavior,上面的断言会通过吗?

最佳答案

这是完美定义的,断言通过了。 c_str() 函数将始终返回有效的以零结尾的 C 字符串。

通常会使用 empty() 来测试空字符串。

关于c++ - 调用空字符串的 c_str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8103342/

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