std::string 容器

发布于 2024-08-13 03:49:04 字数 50 浏览 7 评论 0 原文

std::string 是标准 C++ 库中的容器类,仅限于仅保存 char 元素吗?

Is std::string a container class in standard c++ library, restricted to hold only char elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

烈酒灼喉 2024-08-20 03:49:04

实际上,它是 std::basic_string 的 typedef。 std::basic_string是专门为字符串操作设计的容器类。该容器也可用于宽字符 (wchar_t);对于这种情况,其 typedef 将为 wstring

It's a typedef of std::basic_string<char>, actually. std::basic_string is a container class specifically designed for string operations. This container can be used for wide characters (wchar_t) as well; for that case its typedef would be wstring.

我不会写诗 2024-08-20 03:49:04

std::basic_string<> 是一个非常类似于序列容器的类。请注意,std::basic_string 可以包含任何 POD 类型,而不仅仅是 char 类型的元素(这就是 std:: string 是)或 wchar_t (std::wstring)。

我相信 basic_string 支持序列容器的所有操作。但是,请注意,根据定义,容器类型可以容纳任何可分配和可复制构造的类型,而不仅仅是 POD 类型。所以 basic_string 非常像一个容器,但严格来说它不是一个容器。

换句话说,有些类型可以与容器一起使用,但不能与 basic_string 一起使用。但对于那些可以与 basic_string 一起使用的类型,std::basic_string 提供了序列容器的完整接口(我认为)以及附加功能。

A std::basic_string<> is a class that is very much like a sequence container. Note that std::basic_string can contain any POD type, not just elements of type char (which is what a std::string is) or wchar_t (std::wstring).

I believe that a basic_string supports all the operations of a sequence container. However, note that by definition a container type can hold any assignable and copy-constructable types - not just POD types. So a basic_string is very much like a container, but strictly speaking it's not a container.

In other words there are types that can be used with a container that cannot be used with a basic_string. But for those types that can be used with a basic_string, the std::basic_string provides the full interface of a sequence container (I think) plus additional functionality.

笑,眼淚并存 2024-08-20 03:49:04

std::string 作为 basic_string、std::allocator 的 typedef > 几乎仅限于 char 类型。

但是,我认为 basic_string 本身不一定仅限于字符类型(尽管,顾名思义,它可能旨在用于字符串数据)。

#include <string>
#include <cassert>

int main()
{
    std::basic_string<int> numbers_1, numbers_2;
    numbers_1 += 1;
    numbers_2 += 2;
    std::basic_string<int> numbers_3 = numbers_1 + numbers_2 + 3;
    unsigned pos = numbers_3.find(10);
    assert(pos == std::basic_string<int>::npos);
}

std::string as a typedef for basic_string<char, std::char_traits<char>, std::allocator<char> > is pretty much limited to the char type.

However, I don't think basic_string itself is necessarily limited to only character types (though, as the name suggests, it might be intended to be used for string data).

#include <string>
#include <cassert>

int main()
{
    std::basic_string<int> numbers_1, numbers_2;
    numbers_1 += 1;
    numbers_2 += 2;
    std::basic_string<int> numbers_3 = numbers_1 + numbers_2 + 3;
    unsigned pos = numbers_3.find(10);
    assert(pos == std::basic_string<int>::npos);
}
贱人配狗天长地久 2024-08-20 03:49:04

std::string 是一个 basic_string。
它不一定是 char,但它必须遵循 char 特征
http://www.cplusplus.com/reference/string/char_traits/

std::string is a basic_string.
It's not necessarily a char, but it has to follow the char traits
http://www.cplusplus.com/reference/string/char_traits/

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