gpt4 book ai didi

c++ - std::string 在 C++11 中是否总是以空值结尾?

转载 作者:IT老高 更新时间:2023-10-28 14:00:29 25 4
gpt4 key购买 nike

Herb Sutter 在 2008 年在他的网站上发表的一篇文章中指出:

There is an active proposal to tighten this up further in C++0x and require null-termination and possibly ban copy-on-write implementations, for concurrency-related reasons. Here’s the paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html . I think that one or both of the proposals in this paper is likely to be adopted, but we’ll see at the next meeting or two.

我知道 C++11 现在保证 std::string 内容连续存储,但他们在最终草案中采用了上述内容吗?

现在使用 &str[0] 之类的东西安全吗?

最佳答案

是的。根据 C++0x FDIS 21.4.7.1/1,std::basic_string::c_str() 必须返回

a pointer p such that p + i == &operator[](i) for each i in [0,size()].

这意味着给定一个字符串ss.c_str()返回的指针必须和字符串中起始字符的地址相同(&s[0]).

关于c++ - std::string 在 C++11 中是否总是以空值结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6077189/

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