名稱空間
變體
操作

std::tolower

來自 cppreference.com
< cpp‎ | string‎ | byte
在標頭檔案 <cctype> 中定義
int tolower( int ch );

根據當前安裝的 C 語言環境定義的字元轉換規則,將給定字元轉換為小寫。

在預設的 "C" 語言環境中,以下大寫字母 ABCDEFGHIJKLMNOPQRSTUVWXYZ 被替換為對應的小寫字母 abcdefghijklmnopqrstuvwxyz

目錄

[編輯] 引數

ch - 要轉換的字元。如果 ch 的值不能表示為 unsigned char 且不等於 EOF,則行為未定義。

[編輯] 返回值

ch 的小寫版本,如果當前 C 語言環境中沒有列出小寫版本,則為未修改的 ch

[編輯] 注意

<cctype> 中的所有其他函式一樣,如果引數的值既不能表示為 unsigned char 也不等於 EOF,則 std::tolower 的行為未定義。要安全地將這些函式與普通 char(或 signed char)一起使用,應首先將引數轉換為 unsigned char

char my_tolower(char ch)
{
    return static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
}

同樣,當迭代器值型別為 charsigned char 時,不應直接與標準演算法一起使用它們。而是先將值轉換為 unsigned char

std::string str_tolower(std::string s)
{
    std::transform(s.begin(), s.end(), s.begin(),
                // static_cast<int(*)(int)>(std::tolower)         // wrong
                // [](int c){ return std::tolower(c); }           // wrong
                // [](char c){ return std::tolower(c); }          // wrong
                   [](unsigned char c){ return std::tolower(c); } // correct
                  );
    return s;
}

[編輯] 示例

#include <cctype>
#include <clocale>
#include <iostream>
 
int main()
{
    unsigned char c = '\xb4'; // the character Ž in ISO-8859-15
                              // but ´ (acute accent) in ISO-8859-1
 
    std::setlocale(LC_ALL, "en_US.iso88591");
    std::cout << std::hex << std::showbase;
    std::cout << "in iso8859-1, tolower('0xb4') gives " << std::tolower(c) << '\n';
    std::setlocale(LC_ALL, "en_US.iso885915");
    std::cout << "in iso8859-15, tolower('0xb4') gives " << std::tolower(c) << '\n';
}

可能的輸出

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

[編輯] 另請參閱

將字元轉換為大寫
(函式) [編輯]
使用區域設定的 ctype 刻面將字元轉換為小寫
(函式模板) [編輯]
將寬字元轉換為小寫
(函式) [編輯]
C 文件 用於 tolower

[編輯] 外部連結

1.  ISO/IEC 8859-1。來自維基百科。
2.  ISO/IEC 8859-15。來自維基百科。