C++ wchar_t在一般编程中有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13509733/
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 is the use of wchar_t in general programming?
提问by Kenta
Today I was learning some C++ basics and came to know about wchar_t
. I was not able to figure out, why do we actually need this datatype, and how do I use it?
今天我正在学习一些 C++ 基础知识,并开始了解wchar_t
. 我无法弄清楚,为什么我们实际上需要这种数据类型,我该如何使用它?
采纳答案by ecatmur
wchar_t
is intended for representing text in fixed-width, multi-byteencodings; since wchar_t
is usually 2 bytes in size it can be used to represent text in any 2-byte encoding. It can also be used for representing text in variable-widthmulti-byte encodings of which the most common is UTF-16.
wchar_t
用于以固定宽度、多字节编码表示文本;由于wchar_t
通常大小为 2 个字节,因此可用于表示任何 2 字节编码的文本。它还可以用于表示可变宽度多字节编码中的文本,其中最常见的是 UTF-16。
On platforms where wchar_t
is 4 bytes in size it can be used to represent any text using UCS-4 (Unicode), but since on most platforms it's only 2 bytes it can only represent Unicode in a variable-width encoding (usually UTF-16). It's more common to use char
with a variable-width encoding e.g. UTF-8 or GB 18030.
wchar_t
在大小为 4 个字节的平台上,它可用于表示使用 UCS-4 (Unicode) 的任何文本,但由于在大多数平台上它只有 2 个字节,因此它只能以可变宽度编码(通常是 UTF-16)表示 Unicode . char
与可变宽度编码(例如 UTF-8 或 GB 18030)一起使用更为常见。
About the only modern operating system to use wchar_t
extensively is Windows; this is because Windows adopted Unicode before it was extended past U+FFFF and so a fixed-width 2-byte encoding (UCS-2) appeared sensible. Now UCS-2 is insufficient to represent the whole of Unicode and so Windows uses UTF-16, still with wchar_t
2-byte code units.
唯一wchar_t
广泛使用的现代操作系统是 Windows;这是因为 Windows 在扩展到 U+FFFF 之前就采用了 Unicode,因此固定宽度的 2 字节编码 (UCS-2) 显得合理。现在 UCS-2 不足以表示整个 Unicode,因此 Windows 使用 UTF-16,仍然使用wchar_t
2 字节代码单元。
回答by Agentlien
wchar_t
is a wide character. It is used to represent characters which require more memory to represent them than a regular char
. It is, for example, widely used in the Windows API.
wchar_t
是一个宽字符。它用于表示比常规 需要更多内存来表示它们的字符char
。例如,它广泛用于 Windows API。
However, the size of a wchar_t
is implementation-dependant and not guaranteed to be larger than char
. If you need to support a specific form of character format greater than 8 bits, you may want to turn to char32_t
and char16_t
which are guaranteed to be 32 and 16 bits respectively.
但是, a 的大小wchar_t
取决于实现,并且不能保证大于char
。如果您需要支持大于 8 位的特定形式的字符格式,您可能需要转向char32_t
和char16_t
保证分别为 32 位和 16 位。
回答by intiyaz ahammad shaik
wchar_t
is used when you need to store characters with codes greater than 255 (it has a greater value than char
can store).
wchar_t
当您需要存储代码大于 255 的字符时使用(它的值大于char
可以存储的值)。
char
can take 256 different values which corresponds to entries in the ISO Latin tables. On the other hand, wide char can take more than 65536 values which corresponds to Unicode values. It is a recent international standard which allows the encoding of characters for virtually all languages and commonly used symbols.
char
可以采用 256 个不同的值,对应于 ISO 拉丁语表中的条目。另一方面,宽字符可以采用超过 65536 个对应于 Unicode 值的值。这是最近的国际标准,允许对几乎所有语言和常用符号的字符进行编码。
回答by sohel khalifa
The wchar_tdata type is used to display wide characters that will occupy 16 bits
. This datatype occupies "2 or 4" bytes.
所述wchar_t的数据类型被用来显示wide characters that will occupy 16 bits
。此数据类型占用“2 或 4”个字节。
Mostly the wchar_t
datatype is used when international languages like japanese are used.
wchar_t
当使用日语等国际语言时,主要使用数据类型。
回答by daramarak
The wchar_t type is used for characters of extended character sets. It is among other uses used with wstring which is a string that can hold single characters of extended character sets, as opposed to the string which might hold single characters of size char, or use more than one character to represent a single sign (like utf8).
wchar_t 类型用于扩展字符集的字符。它是与 wstring 一起使用的其他用途,wstring 是一个字符串,可以保存扩展字符集的单个字符,而不是可能保存大小为 char 的单个字符的字符串,或者使用多个字符来表示单个符号(如 utf8 )。
The wchar_t size is dependent on the locales, and is by the standard said to be able to represent all members of the largest extended character set supported by the locales.
wchar_t 大小取决于语言环境,按照标准,据说能够表示语言环境支持的最大扩展字符集的所有成员。