您的位置:首页 > 其它

[wxWidgets]_[初级]_[反转wxString字符串]

2014-05-06 23:29 337 查看
场景:

1. string的反转可以用

#include <algorithm>
void reverse( iterator start, iterator end );


函数,wxString其实也可以,但是可能实现的iterator有bug,对宽字节实现的不够好,wxString内部用unicode存储.所以显示的字符是错的。

可以把wxString转换为wstring再调用reverse,这样还支持宽字节字符串.

代码:

#include <algorithm>
wxString str(L"abcde中文fg");
std::wstring str2 = str.ToStdWstring();
std::reverse(str2.begin(),str2.end());
str = wxString(str2);


测试wxWidgets 3.0.0 直接反转wxString代码:

wxString str(L"abcde中文fg");
std::cout << "str4: " <<  str << std::endl;
std::wstring str2 = str.ToStdWstring();
std::reverse(str2.begin(),str2.end());
std::reverse(str.begin(),str.end());
std::cout << "str3: " <<  str << std::endl;
std::cout << "str2: " <<  str2 << std::endl;
str = wxString(str2);
std::cout << "str1: " <<  str << std::endl;


输出:

str4: abcde中文fg
str3: gf文中e中文fg
str2: gf文中edcba
str1: gf文中edcba


wxWidgets上提的bug: http://trac.wxwidgets.org/ticket/16234
wxWidgets的最新修复,重载swap模版函数替换Generic的实现:

namespace std
{
template <>
void swap<wxUniCharRef>(wxUniCharRef& lhs, wxUniCharRef& rhs)
{
if (&lhs != &rhs)
{
wxUniChar tmp = lhs;
lhs = rhs;
rhs = tmp;
}
}
}


参考:http://www.cplusplus.com/reference/algorithm/swap/

Many components of the standard library (within std) call swap in an unqualified manner
to allow custom overloads for non-fundamental types to be called instead of this generic version:
Custom overloads of swap declared in the same namespace as the type for which they are provided
get selected through argument-dependent lookup over this generic version.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: