std::string src("三毛三毛三毛三毛三三三三流浪记"); size_t size = mbstowcs(NULL,src.c_str(),0); std::wstring dst; dst.resize(size); setlocale(LC_CTYPE,"chs");//setlocale(LC_CTYPE,"UTF-8") on mac mbstowcs(&dst[0],src.c_str(),size); wstring::size_type pos = dst.find_first_of(L'\0'); // L'\0' or wchar_t() dst = dst.substr(0,pos); stable_sort(dst.begin(),dst.end()); wstring::iterator it = unique(dst.begin(),dst.end()); dst.erase(it,dst.end()); size_t newSize = wcstombs(NULL,dst.c_str(),0); string newStr; newStr.resize(newSize); wcstombs(&newStr[0],dst.c_str(),newSize); setlocale(LC_ALL,"C");
wcstombs:
http://msdn.microsoft.com/zh-cn/subscriptions/downloads/5d7tc9zw(v=vs.71).aspx
mbstowcs(NULL,src.c_str(),0) 返回的wstring需要的长度,比实际需要的多
导致wstring.resize的时候,多了许多'\0' ,经过排序去重后,'\0'在最前面
wcstombs 看到'\0' 立马结束,返回string的长度需要0,导致错误。
If wcstombs encounters the wide-character null character (L'\0') either before or when count occurs, it converts it to an 8-bit 0 and stops. Thus, the multibyte character string at mbstr is null-terminated only if wcstombs encounters a wide-character null character during conversion. If the sequences pointed to by wcstrand mbstr overlap, the behavior of wcstombs is undefined.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ezhong的博客签名-------------------------------------
以上内容来自ezhong的博客园,作者:ezhong
ezhong的博客园: http://www.cnblogs.com/ezhong
感谢您的阅读。感谢您的分享。