How to use wchar_t and char16_t, char32_t - C++

First, the origin of the wchar_t type

     We know that the char type variable can store a character of one byte. It can be used to store English characters and punctuation marks, but it cannot be used for Chinese characters, Korean and Japanese characters, because Chinese characters, Korean and Japanese are each character. Occupies two bytes. In order to solve this problem, C++ proposes the wchar_t type, which is called a double-byte type, also known as a wide character type. Wide character constants and wide strings can be denoted by prefixing with L.

Use: wchar_t bob = L'p';

   wcout << L"tall" << endl;

Second, the following is an example

#include <iostream>
#include <locale>//The setlocale function is defined in the locale header file
using namespace std;
int main()
{
 //Use the setlocale function to set the local language to Chinese simplified
 setlocale(LC_ALL,"chs" );//LC_ALL means to set all options (including financial currency, decimal point, time and date format, language string usage habits, etc.), chs means simplified Chinese
 wchar_t wt[] = L"中";//The capital letter L tells The compiler allocates two bytes of space for the "middle" word
 wcout<<wt<<endl;//Use wcout instead of cout to output wide characters
 return 0;
}

3. How to use char16_t and char32_t (C++11)

char16_t  ch1 =u'q';

char32_t ch2 = U'\U0000222B';


All three types have an underlying type (underlying), the choice of which is implementation-dependent, and the underlying type may vary from system to system.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326165072&siteId=291194637