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::stringand std::basic_string? And why are both needed?
std::string和 和有std::basic_string什么区别?为什么两者都需要?
采纳答案by CB Bailey
std::basic_stringis a class template for making strings out of character types, std::stringis a typedeffor 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::stringis an instantiation of std::basic_string<T>:
std::string是一个实例化std::basic_string<T>:
typedef std::basic_string<char> string
std::basic_stringis necessary to have a similar interface for all type of strings (wstringfor example).
std::basic_string必须为所有类型的字符串(wstring例如)提供类似的接口。
回答by Zanson
A std::stringis an instantiation of the std::basic_stringtemplate 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>.

