typedef basic_string<char> String;
typedef basic_string<wchar_t> Wstring;
The former string is a common type and can be viewed as char[], which is exactly the same as the string definition
Consistent with the _elem=char. And wstring, using the wchar_t type, which is wide character, is used to meet the requirements of non-ASCII characters, such as Unicode encoding, Chinese, Japanese, Korean, etc. For wchar_t types, in fact, C + + uses the wchar_t function that corresponds to the char function, because they are defined in a way similar to the one in the same template. Therefore there are wcout, Wcin, Werr and other functions. In fact, string can also be used in Chinese, but it writes a Chinese character in 2 char. If a Chinese character is regarded as a unit wchar_t, then only one unit is occupied in Wstring, and the other non-English characters and encodings are the same. This really satisfies the requirements of the string operation, especially the internationalization of the work.
Look at the following procedure and you will understand the difference between the two.
[CPP]View PlainCopy print?
- #include <iostream>
- #include <string>
- Using namespace std;
- #define TAB "\ T"
- int main ()
- {
- Locale def;
- Cout<<def.name () <<endl;
- Locale current = Cout.getloc ();
- Cout<<current.name () <<endl;
- float val=1234.56; cout<<val<<endl;
- //chage to French/france
- Cout.imbue (the locale ("CHS"));
- Current=cout.getloc ();
- Cout<<current.name () <<endl;
- cout<<val<<endl;
- //above is a description of the usage of the locale, the following is the content of this example, because it uses the Imbue function
- cout<<"*********************************" <<endl;
- // in order to ensure localized output (text/time/currency, etc.), CHS represents China, Wcout must use localized parsing codes
- Wcout.imbue (Std::locale ("CHS"));
- //string English, correct reverse position, display the second character correctly
- String str1 ("Abcabc");
- String Str11 (Str1.rbegin (), Str1.rend ());
- cout<<"uk\ts1\t:" <<str1<<tab<<str1[1]<<tab<<str11<<endl;
- //wstring English, correct reverse position, display the second character correctly
- Wstring str2=l"ABCABC";
- Wstring Str22 (Str2.rbegin (), Str2.rend ());
- wcout<<"uk\tws4\t:" <<str2<<tab<<str2[1]<<tab<<str22<<endl;
- //string Chinese, upside down, becomes garbled, second character read also error
- String Str3 ("How are You?") ");
- String Str33 (Str3.rbegin (), Str3.rend ());
- cout<<"chn\ts3\t:" <<str3<<tab<<str3[1]<<tab<<str33<<endl;
- //method to print the second character correctly
- cout<<"chn\ts3\t:right\t" <<str3[2]<<str3[3]<<endl;
- //Chinese, correct reverse position, display the second character correctly
- Wstring str4=l"How are you?" ";
- Wstring Str44 (Str4.rbegin (), Str4.rend ());
- wcout<<"chn\tws4\t:" <<str4<<tab<<str4[1]<<tab<<str44<<endl;
- //Only string of type char can be constructed like this
- Wstring STR5 (Str1.begin (), Str1.end ());
- Wstring str55 (Str5.rbegin (), Str5.rend ());
- wcout<<"chn\tws5\t:" <<str5<<tab<<str5[1]<<tab<<str55<<endl;
- //This construction will fail!!!!
- Wstring STR6 (Str3.begin (), Str3.end ());
- Wstring str66 (Str6.rbegin (), Str6.rend ());
- wcout<<"chn\tws6\t:" <<str6<<tab<<str6[1]<<tab<<str66<<endl;
- return 0;
- }
The above shows the role of localization, in the number of every three bits plus a comma, in fact, time/text and so on are affected.
The following output shows how to correctly use the string and Wstring methods. The third is because a string is used to represent Chinese characters, and some errors occur. The last line is also an error, causing the output to also be affected, with no spaces and carriage returns. (The last two will not be in English, just explain the Chinese construction method is wrong)
"Mastering the standard C + + class" in the 12th chapter "Language support" specifically in C + + internationalization and localization issues, C + + provides i18n standard processing, software developers can refer to. C + + Standard library is also very profound, functional relatively complete. Continue to learn.
From: http://hi.baidu.com/janvyking999/blog/item/fd5d44df572c3c5c94ee37de.html
[C + +] C + + standard in string and wstring