当前位置:   article > 正文

linux 字符串转大写字母,linux C++ string大小写转换

string 类型大小写 linux toupper

#include

#include

#include

#include

using namespace std;

int main()

{

string s = "ddkfjsldjl";

transform(s.begin(), s.end(), s.begin(), toupper);

cout<

return 0;

}

但在使用g++编译时会报错:

对 ‘transform(__gnu_cxx::__normal_iterator, std::allocator > >, __gnu_cxx::__normal_iterator, std::allocator > >, __gnu_cxx::__normal_iterator, std::allocator > >, )’ 的调用没有匹配的函数。

这里出现错误的原因是Linux将toupper实现为一个宏而不是函数:

/usr/lib/syslinux/com32/include/ctype.h:

/* Note: this is decimal, not hex, to avoid accidental promotion to unsigned */

#define _toupper(__c) ((__c) & ~32)

#define _tolower(__c) ((__c) | 32)

__ctype_inline int toupper(int __c)

{

return islower(__c) ? _toupper(__c) : __c;

}

__ctype_inline int tolower(int __c)

{

return isupper(__c) ? _tolower(__c) : __c;

}

两种解决方案:

1.transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);

这里(int (*)(int))toupper是将toupper转换为一个返回值为int,参数只有一个int的函数指针。

2.自己实现ToUpper函数:

int ToUpper(int c)

{

return toupper(c);

}

transform(str.begin(), str.end(), str.begin(), ToUpper);

附:大小写转换函数

#include

#include

#include

using namespace std;

void ToUpperString(string &str)

{

transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);

}

void ToLowerString(string &str) {     transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower); }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/222608
推荐阅读
相关标签
  

闽ICP备14008679号