从 std::string_view 创建 std::string
给定一个 string_view sv
和一个 string s(sv)
,s
是否在内部使用与 sv
相同的 char 数组?可以肯定地说,当s
被销毁时,sv
仍然有效吗?
Given a string_view sv
and a string s(sv)
, does s
use the same char array internally as sv
? Is it safe to say that when s
is destroyed, sv
is still valid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建
std :: String
Object 始终 copies(或移动(如果可以的话)),并在内部处理自己的内存。就您的示例而言,由
sv
和s
处理的字符串完全不同且分开。Creating a
std::string
object always copies (or moves, if it can) the string, and handles its own memory internally.For your example, the strings handled by
sv
ands
are totally different and separate.只需运行此演示程序即可。
它的输出可能看起来像是
您可以看到字符串文字的地址与
std :: string_view
data extale返回的内存的地址相同。代码>。那就是类
std :: string_view
只是围绕带下划线的引用对象的包装器。至于类
std :: String
然后创建通常存储在分配的内存中的字符串的副本。例如,您可能不会更改类型
std :: String_view
的对象,但是std :: String
是专门设计的。后缀
view
在类名称std :: string_view
中表示您只能查看typestd :: string_view 参考。
Just run this demonstration program.
Its output might look like
As you can see the address of the string literal is the same as the address of the memory returned by the data member
data
of the object ofstd::string_view
.That is the class
std::string_view
is just a wrapper around the underlined referenced object.As for the class
std::string
then it creates its one copy of a string usually stored in the allocated memory.For example you may not change an object of the type
std::string_view
butstd::string
is designed specially that process stored strings.The suffix
view
in the class namestd::string_view
means that you can only view the underlined object to which an object of the typestd::string_view
refers.