[C + +] C + + standard in string and wstring

Source: Internet
Author: User
Tags locale

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?
  1. #include <iostream>
  2. #include <string>
  3. Using namespace std;
  4. #define TAB "\ T"
  5. int main ()
  6. {
  7. Locale def;
  8. Cout<<def.name () <<endl;
  9. Locale current = Cout.getloc ();
  10. Cout<<current.name () <<endl;
  11. float val=1234.56;   cout<<val<<endl;
  12. //chage to French/france
  13. Cout.imbue (the locale ("CHS"));
  14. Current=cout.getloc ();
  15. Cout<<current.name () <<endl;
  16. cout<<val<<endl;
  17. //above is a description of the usage of the locale, the following is the content of this example, because it uses the Imbue function
  18. cout<<"*********************************" <<endl;
  19. // in order to ensure localized output (text/time/currency, etc.), CHS represents China, Wcout must use localized parsing codes
  20. Wcout.imbue (Std::locale ("CHS"));
  21. //string English, correct reverse position, display the second character correctly
  22. String str1 ("Abcabc");
  23. String Str11 (Str1.rbegin (), Str1.rend ());
  24. cout<<"uk\ts1\t:" <<str1<<tab<<str1[1]<<tab<<str11<<endl;
  25. //wstring English, correct reverse position, display the second character correctly
  26. Wstring str2=l"ABCABC";
  27. Wstring Str22 (Str2.rbegin (), Str2.rend ());
  28. wcout<<"uk\tws4\t:" <<str2<<tab<<str2[1]<<tab<<str22<<endl;
  29. //string Chinese, upside down, becomes garbled, second character read also error
  30. String Str3 ("How are You?")   ");
  31. String Str33 (Str3.rbegin (), Str3.rend ());
  32. cout<<"chn\ts3\t:" <<str3<<tab<<str3[1]<<tab<<str33<<endl;
  33. //method to print the second character correctly
  34. cout<<"chn\ts3\t:right\t" <<str3[2]<<str3[3]<<endl;
  35. //Chinese, correct reverse position, display the second character correctly
  36. Wstring str4=l"How are you?"   ";
  37. Wstring Str44 (Str4.rbegin (), Str4.rend ());
  38. wcout<<"chn\tws4\t:" <<str4<<tab<<str4[1]<<tab<<str44<<endl;
  39. //Only string of type char can be constructed like this
  40. Wstring STR5 (Str1.begin (), Str1.end ());
  41. Wstring str55 (Str5.rbegin (), Str5.rend ());
  42. wcout<<"chn\tws5\t:" <<str5<<tab<<str5[1]<<tab<<str55<<endl;
  43. //This construction will fail!!!!
  44. Wstring STR6 (Str3.begin (), Str3.end ());
  45. Wstring str66 (Str6.rbegin (), Str6.rend ());
  46. wcout<<"chn\tws6\t:" <<str6<<tab<<str6[1]<<tab<<str66<<endl;
  47. return 0;
  48. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.