C++ 字符串转大写:使用 toupper 函数
可以使用标准库中的 'toupper' 函数将字符串转换为大写。具体实现如下:
#include <iostream>
#include <string>
#include <algorithm> // for toupper
int main() {
std::string str = 'Hello World';
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
std::cout << str << std::endl; // 输出 'HELLO WORLD'
return 0;
}
在上述代码中,我们使用了 'std::transform' 函数来对字符串中的每个字符进行转换。函数的第一个参数是字符串的起始位置,第二个参数是字符串的结束位置,第三个参数是转换后的结果。第四个参数是 '::toupper' 函数,表示将字符转换为大写形式。
原文地址: https://www.cveoy.top/t/topic/n1qF 著作权归作者所有。请勿转载和采集!