std::string 容器
std::string 是标准 C++ 库中的容器类,仅限于仅保存 char 元素吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
std::string 是标准 C++ 库中的容器类,仅限于仅保存 char 元素吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
实际上,它是
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 bewstring
.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 thatstd::basic_string
can contain any POD type, not just elements of typechar
(which is what astd::string
is) orwchar_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 abasic_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 abasic_string
, thestd::basic_string
provides the full interface of a sequence container (I think) plus additional functionality.std::string 作为
basic_string、std::allocator 的 typedef >
几乎仅限于 char 类型。但是,我认为 basic_string 本身不一定仅限于字符类型(尽管,顾名思义,它可能旨在用于字符串数据)。
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).
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/