transform(str.begin(), str.end(), str.begin(),tolower)编译失败

解决方法有以下三种:
1.transform(str.begin(), str.end(), str.begin(), ::tolower)
2.transform((str.begin(), str.end(), str.begin() begin(s), static_cast<int(*)(int)>(tolower))
3.transform(transform(str.begin(), str.end(), str.begin(), [](const unsigned char i){ return tolower(i); })

产生问题的原因是<locale><cctype>中都定义了这个tolower,并且都在std namespace里面。
locale :template <typename T> T tolower(T, const locale&)
cctype:int tolower(int)

解决思路:
1.locale中定义的tolower不在global namespace里面。
2.用static_cast强制转换消除不确定性。
3.用lamda接收unsigned char,然后传给tolower。

第三种方法是最好的方法,原因如下:
1.第一种方法成功的的条件是cctype定义的tolower要在global namespace里面,但是这是不确定的。

It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3)

2 如果传给tolower的参数不能表示为unsigned char,那结果是不确定的。
char 表示signed还是unsigned 是取决于具体的实现的,是不确定的。而string的元素就是char,所以把string 的元素传给tolower可能导致不确定的结果。
解决办法是之前做一步强制类型转换。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值