std::toupper_C++中文网

按照当前安装的 C 本地环境所定义的字符转换规则,转换给定字符为大写。

默认 "C" 本地环境中,分别以大写字母 ABCDEFGHIJKLMNOPQRSTUVWXYZ 替换下列小写字母 abcdefghijklmnopqrstuvwxyz

参数

ch - 要转化的字符。若 ch 的值不能表示为 unsigned char 且不等于 EOF ,则行为未定义。

返回值

被转换的字符,或若当前 C 本地环境不定义大写版本则为 ch

注意

同所有其他来自 <cctype> 的函数,若参数值既不能表示为 unsigned char 又不等于 EOFstd::toupper 的行为未定义。为了以简单的 char (或 signed char )安全使用此函数,首先要将参数转换为 unsigned char

char my_toupper(char ch)
{
    return static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
}

类似地,迭代器的值类型为 charsigned char 时,不应直接将它们用于标准算法。而是要首先转换值为 unsigned char

std::string str_toupper(std::string s) {
    std::transform(s.begin(), s.end(), s.begin(), 
                // static_cast<int(*)(int)>(std::toupper)         // 错误
                // [](int c){ return std::toupper(c); }           // 错误
                // [](char c){ return std::toupper(c); }          // 错误
                   [](unsigned char c){ return std::toupper(c); } // 正确
                  );
    return s;
}

示例

输出:

in iso8859-1, toupper('0xb8') gives 0xb8
in iso8859-15, toupper('0xb8') gives 0xb4

参阅