对std::string和std::wstring区别的解释,807个赞同,有例子
807down vote
|
|
|
@Sorin Sbarnea: UTF-8 could take 1-6 bytes, but apparently the standard limits it to 1-4. See en.wikipedia.org/wiki/UTF8#Description for more information. – paercebal Jan 13 '10 at 13:10
|
||
|
While this examples produces different results on Linux and Windows the C++ program contains implementation-defined behavior as to whether
olè is encoded as UTF-8 or not. Further more, the reason you cannot natively stream wchar_t * to std::cout is because the types are incompatible resulting in an ill-formed program and it has nothing to do with the use of encodings. It's worth pointing out that whether you use std::string or std::wstring depends on your own encoding preference rather than the platform, especially if you want your code to be portable. – John Leidegren Aug 9 '12 at 9:37 |
||
|
@paercebal Whatever the platform supports is entirely arbitrary and besides the point. If you store all strings internally as UTF-8 on Windows you'll have to convert them to either ANSI or UTF-16 and call the corresponding Win32 function but if you know your UTF-8 strings are just plain ASCII strings you don't have to do anything. The platform doesn't dictate how you use strings as much as the circumstances. – John Leidegren Aug 9 '12 at 16:35
|
||
|
Windows actually uses UTF-16 and have been for quite some time, older versions of Windows did use UCS-2 but this is not the case any longer. My only issue here is the conclusion that
std::wstring should be used on Windows because it's a better fit for the Unicode Windows API which I think is fallacious. If your only concern was calling into the Unicode Windows API and not marshalling strings then sure but I don't buy this as the general case. – John Leidegren Aug 9 '12 at 18:15 |
||
|
@ John Leidegren :
If your only concern was calling into the Unicode Windows API and not marshalling strings then sure : Then, we agree. I'm coding in C++, not JavaScript. Avoiding useless marshalling or any other potentially costly processing at runtime when it can be done at compile time is at the heart of that language. Coding against WinAPI and using std::string is just an unjustified wasting runtime resources. You find it fallacious, and it's Ok, as it is your viewpoint. My own is that I won't write code with pessimization on Windows just because it looks better from the Linux side. – paercebal Aug 9 '12 at 19:48 |
![](https://engine.adzerk.net/i.gif?e=eyJhdiI6MjYxMiwiYXQiOjQsImJ0IjowLCJjbSI6NjM1ODYwLCJjaCI6MTE3OCwiY2siOnt9LCJjciI6Mzc5Mzc3OCwiZGkiOiIzYWE1Y2NlN2UzMWU0YTYxOWRhMTZkMjYzM2JlMjMzOSIsImRtIjoxLCJmYyI6NjMxODU5NSwiZmwiOjYzNDUwNDUsImlwIjoiNjIuMjMuMTQyLjEwMSIsImt3IjoiYysrLHN0cmluZyx1bmljb2RlLGMrKy1mYXEsd3N0cmluZyx4LWNyb3NzLXNpdGUtYXNrdWJ1bnR1LmNvbSIsIm53IjoyMiwicGMiOjAsImVjIjowLCJwciI6NDI1MzAsInJ0IjoyLCJyZiI6Imh0dHBzOi8vd3d3Lmdvb2dsZS5mci8iLCJzYSI6IjgiLCJzYiI6ImktMGEyYzEwZWI4OTA4MTMzNWUiLCJzcCI6ODU4MSwic3QiOjgyNzcsInVrIjoidWUxLWExZGM4MmFiNjAwZTRjNDE4MDA5MGJjZTZkYmFmMDlkIiwiem4iOjQ0LCJ0cyI6MTUwNjMyODcwNDIyMCwiYmYiOnRydWUsInBuIjoiY2xjLW1sYiIsImZxIjowfQ&s=TOf3DEI14w74ajV32tKIcinW6N8)
So, every reader here now should have a clear understanding about the facts, the situation. If not, then you must read paercebal's outstandingly comprehensive answer [btw: thanks!]. My pragmatical conclusion is shockingly simple: all that C++ (and STL) "character encoding" stuff is substantially broken and useless. Blame it on Microsoft or not, that will not help anyway. My solution, after in-depth investigation, much frustration and the consequential experiences is the following:
The conversions are straightforward, google should help here ... That's it. Use UTF8String wherever memory is precious and for all UTF-8 I/O. Use UCS2String wherever the string must be parsed and/or manipulated. You can convert between those two representations any time. Alternatives & Improvements
ICU or other unicode libraries? |
|||||||||||||||||||||
|
I recommend avoiding My view is summarized in http://utf8everywhere.org of which I am a co-author. Unless your application is API-call-centric, e.g. mainly UI application, the suggestion is to store Unicode strings in std::string and encoded in UTF-8, performing conversion near API calls. The benefits outlined in the article outweigh the apparent annoyance of conversion, especially in complex applications. This is doubly so for multi-platform and library development. And now, answering your questions:
|
||||
|
|||||||||||||||||||||
|
I frequently use std::string to hold utf-8 characters without any problems at all. I heartily recommend doing this when interfacing with API's which use utf-8 as the native string type as well. For example, I use utf-8 when interfacing my code with the Tcl interpreter. The major caveat is the length of the std::string, is no longer the number of characters in the string.
|
|||||||||||||||||||||
|
|
|||||||||||||
|
Applications that are not satisfied with only 256 different characters have the options of either using wide characters (more than 8 bits) or a variable-length encoding (a multibyte encoding in C++ terminology) such as UTF-8. Wide characters generally require more space than a variable-length encoding, but are faster to process. Multi-language applications that process large amounts of text usually use wide characters when processing the text, but convert it to UTF-8 when storing it to disk. The only difference between a Practically every compiler uses a character set whose first 128 characters correspond with ASCII. This is also the case with compilers that use UTF-8 encoding. The important thing to be aware of when using strings in UTF-8 or some other variable-length encoding, is that the indices and lengths are measured in bytes, not characters. The data type of a wstring is If you don't need multi-language support, you might be fine with using only regular strings. On the other hand, if you're writing a graphical application, it is often the case that the API supports only wide characters. Then you probably want to use the same wide characters when processing the text. Keep in mind that UTF-16 is a variable-length encoding, meaning that you cannot assume |
|||||||||||||||||
|
1) As mentioned by Greg, wstring is helpful for internationalization, that's when you will be releasing your product in languages other than english 4) Check this out for wide character http://en.wikipedia.org/wiki/Wide_character |
|||
|
|||||||||||||||||||||
|
A good question! I think DATA ENCODING (sometime CHARSET also involved) is a MEMORY EXPRESSION MECHANISM in order to save data to file or transfer data via network, so I answer this question as: 1.When should I use std::wstring over std::string? If the programming platform or API function is a single-byte one, and we want to process or parse some unicode datas, e.g read from Windows' .REG file or network 2-byte stream, we should declare std::wstring variable to easy process them. e.g.: wstring ws=L"中国a"(6 octets memory: 0x4E2D 0x56FD 0x0061), we can use ws[0] to get character '中' and ws[1] to get character '国' and ws[2] to get character 'a', etc. 2.Can std::string hold the entire ASCII character set, including the special characters? Yes. But notice: American ASCII, means each 0x00~0xFF octet stand for one character ,including printable text such as "123abc&*_&" and you said special one, mostly print it as a '.' avoid confusing editors or terminals. And some other countries extend their own "ASCII" charset ,e.g. Chinese, use 2 octets to stand for one character. 3.Is std::wstring supported by all popular C++ compilers? Maybe, or mostly. I have used: VC++6 and GCC 3.3, YES 4.What is exactly a "wide character"? wide character mostly indicate using 2 octets or 4 octets to hold all countries's characters. 2 octets UCS2 is a representative sample, and further e.g. English 'a', its memory is 2 octet of 0x0061(vs in ASCII 'a's memory is 1 octet 0x61) |
https://stackoverflow.com/questions/402283/stdwstring-vs-stdstring
对std::string和std::wstring区别的解释,807个赞同,有例子的更多相关文章
- C++ MFC std::string转为 std::wstring
std::string转为 std::wstring std::wstring UTF8_To_UTF16(const std::string& source) { unsigned long ...
- 如何使用 window api 转换字符集?(std::string与std::wstring的相互转换)
//宽字符转多字节 std::string W2A(const std::wstring& utf8) { int buffSize = WideCharToMultiByte(CP_ACP, ...
- std::string与std::wstring互相转换
作者:zzandyc来源:CSDN原文:https ://blog.csdn.net/zzandyc/article/details/77540056 版权声明:本文为博主原创文章,转载请附上博文链接 ...
- std::u32string conversion to/from std::string and std::u16string
I need to convert between UTF-8, UTF-16 and UTF-32 for different API's/modules and since I know have ...
- std::string, std::wstring, wchar_t*, Platform::String^ 之间的相互转换
最近做WinRT的项目,涉及到Platform::String^ 和 std::string之间的转换,总结一下: (1)先给出源代码: std::wstring stows(std::string ...
- std::wstring std::string w2m m2w
static std::wstring m2w(std::string ch, unsigned int CodePage = CP_ACP) { if (ch.empty())return L&qu ...
- C++ std::unordered_map使用std::string和char *作key对比
最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...
- std::string 用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...
随机推荐
- php实现反转链表(链表题一定记得画图)(指向链表节点的指针本质就是一个记录地址的变量)($p->next表示的是取p节点的next域里面的数值,next只是p的一个属性)
php实现反转链表(链表题一定记得画图)(指向链表节点的指针本质就是一个记录地址的变量)($p->next表示的是取p节点的next域里面的数值,next只是p的一个属性) 一.总结 链表反转两 ...
- 使用truss、strace或ltrace诊断软件的"疑难杂症"
原文链接 简介 进程无法启动,软件运行速度突然变慢,程序的"Segment Fault"等等都是让每个Unix系统用户头痛的问题,本文通过三个实际案例演示如何使用truss.str ...
- XxPay支付系统-boot版本了解一下
了解一下 之前看了龙果支付系统,也没看透,用公司框架改写,然后就改的比较乱
- php实现 密码验证合格程序(复杂问题分类,超简单的)(分类+规范编码)
php实现 密码验证合格程序(复杂问题分类,超简单的)(分类+规范编码) 一.总结 一句话总结:复杂问题分类,超简单的.分类+规范编码. 1.写的时候判断 不能有相同长度超2的子串重复 的时候,子 ...
- 【u009】瑞瑞的木板
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每 ...
- [Vue] Create Filters in Vue.js
Just like in the command line, you can pipe a property through a filter to get a desired result. You ...
- 网络编程02---HTTP协议
1.URL简单介绍 1.client怎样找到server 我们都知道网络中部署着各种各样的server.比方腾讯的server.百度的server.那么问题来了.client怎样找到想要连接的serv ...
- js如何实现动态的在表格中添加和删除行?(两种方法)
js如何实现动态的在表格中添加和删除行?(两种方法) 一.总结 1.table元素有属性和一些方法(js使用) 方法一:添加可通过在table的innerHTML属性中添加tr和td来实现 tab.i ...
- alloc init初始化后对象依然还在父视图
self.TableView=[[UITableView alloc]init]; ........2个cell //下面但方法和addsubviews方法不一样 [self.view insertS ...
- springMVC中前台ajax传json数据后台controller接受对象为null
在jquery的ajax中,如果没加contentType:"application/json",那么data就应该对应的是json对象,反之,如果加了contentType:&q ...