C/C++字符串大小写转换

windows环境下,可以使用algorithm头文件中的toupper:

#include   
#include   
#include   
#include   
using namespace std;  
int main()  
{  
    string s = "ddkfjsldjl";  
    transform(s.begin(), s.end(), s.begin(), toupper);  
    cout<<s<<endl;  
    return 0;  
}  

代码来自:https://blog.csdn.net/jiafu1115/article/details/18189123

而Linux环境下,由于Linux将toupper实现为一个宏而不是函数,故而此时 C++中没有string直接转换大小写的函数,需要自己实现。

###方案一:

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

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

###方案二:

int ToUpper(int c)  
{  
    return toupper(c);  
}  
transform(str.begin(), str.end(), str.begin(), ToUpper);  

Linux中使用原函数会报错:


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


参考内容:https://blog.csdn.net/jiafu1115/article/details/18189123

你可能感兴趣的:(C/C++)