C ++文字和Unicode [英] C++ Literals and Unicode
问题描述
C ++文字
环境:
- 操作系统:Windows 10 Pro;
- 编译器:最新的GCC.
- IDE:代码::阻止最新访问.
- 正在使用:控制台应用程序.
我对数字文字前缀的理解是,它们对确定数值类型很有用(不确定).但是,我对字符和字符串文字前缀和后缀有很多困惑.我读了很多书,花了几天的时间试图了解这种情况,但是我有更多的问题和很少的答案.所以我认为堆栈溢出可能有很多帮助.
My understanding for numerical literals prefixes is that they are useful to determine the numerical value type (not sure).However, I have a lot of confusion on character and string literals prefixes and suffixes. I read a lot and spent days trying to understand the situation, but I got more questions and few answers. so I thought stack overflow could be of a lot of help.
问:
1-字符串前缀u8 u U L的正确用法是什么?
1- What are the correct use for the string prefixes u8 u U L?
我以以下代码为例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "\n\n Hello World! (plain) \n";
cout << u8"\n Hello World! (u8) \n";
cout << u"\n Hello World! (u) \n";
cout << U"\n Hello World! (U) \n";
cout << L"\n Hello World! (plain) \n\n";
cout << "\n\n\n";
}
输出如下:
Hello World! (普通)
Hello World! (plain)
世界您好! (u8)
0x47f0580x47f0840x47f0d8
0x47f0580x47f0840x47f0d8
Q2:为什么U u ans L具有这样的输出?我希望这只是确定类型而不进行编码映射(如果是).
Q2: Why U u ans L has such output? I expected it is just to determine type not do encoding mapping (if it is).
Q3是否有关于UTF-8之类的编码的简单而要点的参考.我对它们感到困惑,此外,我怀疑控制台应用程序是否能够处理它们.我认为了解它们至关重要.
Q3 Is there a simple and to the point references about encodings like UTF-8. I am confused about them, in addition I doubt that console applications is capable to deal with them. I see it is crucial to understand them.
Q4:另外,我将感谢您逐步解释自定义类型文字的参考.
Q4: Also I will appreciate a step by step reference that explain custom type literals.
推荐答案
首先查看: http://en.cppreference.com/w/cpp/language/string_literal
std::cout
的类运算符<<
已正确重载以打印const char*
.这就是为什么打印前两个字符串的原因.
std::cout
's class operator <<
is properly overloaded to print const char*
. That is why the first two strings are printed.
cout << "\n\n Hello World! (plain) \n";
cout << u8"\n Hello World! (u8) \n";
按预期,打印 1 :
Hello World! (plain)
Hello World! (u8)
同时std::cout
的类对于const char16_t*
,const char32_t*
和const wchar_t*
没有特殊的<<
重载,因此它将与<<
的重载匹配以打印指针,这就是为什么:>
Meanwhile std::cout
's class has no special <<
overload for const char16_t*
, const char32_t*
and const wchar_t*
, hence it will match <<
's overload for printing pointers, that is why:
cout << u"\n Hello World! (u) \n";
cout << U"\n Hello World! (U) \n";
cout << L"\n Hello World! (plain) \n\n";
打印:
0x47f0580x47f0840x47f0d8
如您所见,实际上在那里打印了3个指针值:0x47f058
,0x47f084
和0x47f0d8
As you can see, there are actually 3 pointer values printed there: 0x47f058
, 0x47f084
and 0x47f0d8
但是,对于最后一个,您可以使用std::wcout
However, for the last one, you can get it to print properly using std::wcout
std::wcout << L"\n Hello World! (plain) \n\n";
打印
Hello World! (plain)
1:由于直接 查看全文