从 std::string_view 创建 std::string

发布于 2025-01-21 03:51:09 字数 170 浏览 0 评论 0原文

给定一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2025-01-28 03:51:09

创建std :: String Object 始终 copies(或移动(如果可以的话)),并在内部处理自己的内存。

就您的示例而言,由svs处理的字符串完全不同且分开。

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 and s are totally different and separate.

在你怀里撒娇 2025-01-28 03:51:09

只需运行此演示程序即可。

#include <iostream>
#include <string>
#inc,lude <string_view>

int main()
{
    const char *s = "Hello World!";

    std::cout << "The address of the string literal is "
        << static_cast< const void * >( s ) << '\n';

    std::string_view sv( s );

    std::cout << "The address of the object of the type std::string_view is "
        << static_cast< const void * >( sv.data() ) << '\n';

    std::string ss( sv );

    std::cout << "The address of the string is "
        << static_cast< const void * >( ss.data() ) << '\n';
}

它的输出可能看起来像是

The address of the string literal is 00694E6C
The address of the object of the type std::string_view is 00694E6C
The address of the string is 0133F7C0

您可以看到字符串文字的地址与std :: string_view data extale返回的内存的地址相同。代码>。

那就是类std :: string_view只是围绕带下划线的引用对象的包装器。

至于类std :: String然后创建通常存储在分配的内存中的字符串的副本。

例如,您可能不会更改类型std :: String_view的对象,但是std :: String是专门设计的。

后缀view在类名称std :: string_view中表示您只能查看type std :: string_view 参考。

Just run this demonstration program.

#include <iostream>
#include <string>
#inc,lude <string_view>

int main()
{
    const char *s = "Hello World!";

    std::cout << "The address of the string literal is "
        << static_cast< const void * >( s ) << '\n';

    std::string_view sv( s );

    std::cout << "The address of the object of the type std::string_view is "
        << static_cast< const void * >( sv.data() ) << '\n';

    std::string ss( sv );

    std::cout << "The address of the string is "
        << static_cast< const void * >( ss.data() ) << '\n';
}

Its output might look like

The address of the string literal is 00694E6C
The address of the object of the type std::string_view is 00694E6C
The address of the string is 0133F7C0

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 of std::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 but std::string is designed specially that process stored strings.

The suffix view in the class name std::string_view means that you can only view the underlined object to which an object of the type std::string_view refers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文