C++ std::string 和 std::basic_string 有什么区别?为什么两者都需要?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1662107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between std::string and std::basic_string? And why are both needed?
提问by Benj
What's the difference between std::string
and std::basic_string
? And why are both needed?
std::string
和 和有std::basic_string
什么区别?为什么两者都需要?
采纳答案by CB Bailey
std::basic_string
is a class template for making strings out of character types, std::string
is a typedef
for a specialization of that class template for char
. Yes they are both needed (or at least required by the standard).
std::basic_string
是用于从字符类型中生成字符串的类模板,std::string
是typedef
用于char
. 是的,它们都是需要的(或者至少是标准要求的)。
回答by pmr
std::string
is an instantiation of std::basic_string<T>
:
std::string
是一个实例化std::basic_string<T>
:
typedef std::basic_string<char> string
std::basic_string
is necessary to have a similar interface for all type of strings (wstring
for example).
std::basic_string
必须为所有类型的字符串(wstring
例如)提供类似的接口。
回答by Zanson
A std::string
is an instantiation of the std::basic_string
template with a type of char
. You need both so that you can make strings of things besides char
, such a std::basic_string<wchar_t>
for a string of wide characters. Or if you want a string with 32 bit elements, std::basic_string<unsigned int>
.
Astd::string
是std::basic_string
模板的实例化,类型为char
。您需要两者,以便您可以制作除 之外的字符串char
,例如std::basic_string<wchar_t>
用于宽字符的字符串。或者,如果您想要一个包含 32 位元素的字符串,std::basic_string<unsigned int>
.