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


场景:

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.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peter(阿斯拉达)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值