wstring

How to initialize and print a std::wstring?

我与影子孤独终老i 提交于 2019-11-26 20:08:57
问题 I had the code: std::string st = "SomeText"; ... std::cout << st; and that worked fine. But now my team wants to move to wstring . So I tried: std::wstring st = "SomeText"; ... std::cout << st; but this gave me a compilation error: Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'const char [8]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' D:...\TestModule1.cpp 28 1 TestModule1 After

Read Unicode UTF-8 file into wstring

前提是你 提交于 2019-11-26 17:36:15
问题 How can I read a Unicode (UTF-8) file into wstring (s) on the Windows platform? 回答1: With C++11 support, you can use std::codecvt_utf8 facet which encapsulates conversion between a UTF-8 encoded byte string and UCS2 or UCS4 character string and which can be used to read and write UTF-8 files, both text and binary. In order to use facet you usually create locale object that encapsulates culture-specific information as a set of facets that collectively define a specific localized environment.

Case insensitive std::string.find()

安稳与你 提交于 2019-11-26 17:27:50
问题 I am using std::string 's find() method to test if a string is a substring of another. Now I need case insensitive version of the same thing. For string comparison I can always turn to stricmp() but there doesn't seem to be a stristr() . I have found various answers and most suggest using Boost which is not an option in my case. Additionally, I need to support std::wstring / wchar_t . Any ideas? 回答1: You could use std::search with a custom predicate. #include <locale> #include <iostream>

Convert wstring to string encoded in UTF-8

房东的猫 提交于 2019-11-26 10:56:03
问题 I need to convert between wstring and string. I figured out, that using codecvt facet should do the trick, but it doesn\'t seem to work for utf-8 locale. My idea is, that when I read utf-8 encoded file to chars, one utf-8 character is read into two normal characters (which is how utf-8 works). I\'d like to create this utf-8 string from wstring representation for library I use in my code. Does anybody know how to do it? I already tried this: locale mylocale(\"cs_CZ.utf-8\"); mbstate_t mystate;

What&#39;s “wrong” with C++ wchar_t and wstrings? What are some alternatives to wide characters?

 ̄綄美尐妖づ 提交于 2019-11-26 10:16:49
I have seen a lot of people in the C++ community(particularly ##c++ on freenode) resent the use of wstrings and wchar_t , and their use in the windows api. What is exactly "wrong" with wchar_t and wstring , and if I want to support internationalization, what are some alternatives to wide characters? bames53 What is wchar_t? wchar_t is defined such that any locale's char encoding can be converted to a wchar_t representation where every wchar_t represents exactly one codepoint: Type wchar_t is a distinct type whose values can represent distinct codes for all members of the largest extended

C++ Convert string (or char*) to wstring (or wchar_t*)

廉价感情. 提交于 2019-11-25 23:33:52
问题 string s = \"おはよう\"; wstring ws = FUNCTION(s, ws); How would i assign the contents of s to ws? Searched google and used some techniques but they can\'t assign the exact content. The content is distorted. 回答1: Assuming that the input string in your example (おはよう) is a UTF-8 encoded (which it isn't, by the looks of it, but let's assume it is for the sake of this explanation :-)) representation of a Unicode string of your interest, then your problem can be fully solved with the standard library

std::wstring VS std::string

社会主义新天地 提交于 2019-11-25 22:19:14
问题 I am not able to understand the differences between std::string and std::wstring . I know wstring supports wide characters such as Unicode characters. I have got the following questions: When should I use std::wstring over std::string ? Can std::string hold the entire ASCII character set, including the special characters? Is std::wstring supported by all popular C++ compilers? What is exactly a \" wide character \"? 回答1: string ? wstring ? std::string is a basic_string templated on a char ,