std::toupper
From cppreference.com
| ヘッダー <cctype> で定義 |
||
| int toupper( int ch ); |
||
現在インストールされているCロケールで定義されている文字変換規則に従って、指定された文字を大文字に変換します。
デフォルトの "C" ロケールでは、小文字 `abcdefghijklmnopqrstuvwxyz` はそれぞれ大文字 `ABCDEFGHIJKLMNOPQRSTUVWXYZ` に置き換えられます。
目次 |
[編集] パラメータ
| 文字 | - | 変換される文字。 ch の値が unsigned char として表現できず、かつ EOF と等しくない場合、動作は未定義です。 |
[編集] 戻り値
変換された文字。現在のCロケールで大文字バージョンが定義されていない場合は ch。
[編集] 注
<cctype> の他のすべての関数と同様に、引数の値が unsigned char として表現できず、かつ EOF と等しくない場合、std::toupper の動作は未定義です。プレーンな char (または signed char)でこれらの関数を安全に使用するには、まず引数を unsigned char に変換する必要があります。
char my_toupper(char ch) { return static_cast<char>(std::toupper(static_cast<unsigned char>(ch))); }
同様に、イテレータの値型がcharまたはsigned charである場合、標準アルゴリズムで直接使用しないでください。代わりに、まず値をunsigned charに変換してください。
std::string str_toupper(std::string s) { std::transform(s.begin(), s.end(), s.begin(), // static_cast<int(*)(int)>(std::toupper) // wrong // [](int c){ return std::toupper(c); } // wrong // [](char c){ return std::toupper(c); } // wrong [](unsigned char c){ return std::toupper(c); } // correct ); return s; }
[編集] 例
このコードを実行
#include <cctype> #include <climits> #include <clocale> #include <iostream> #include <ranges> int main() { for (auto bd{0}; unsigned char lc : std::views::iota(0, UCHAR_MAX)) if (unsigned char uc = std::toupper(lc); uc != lc) std::cout << lc << uc << (13 == ++bd ? '\n' : ' '); std::cout << "\n\n"; unsigned char c = '\xb8'; // the character ž in ISO-8859-15 // but ¸ (cedilla) in ISO-8859-1 std::setlocale(LC_ALL, "en_US.iso88591"); std::cout << std::hex << std::showbase; std::cout << "in iso8859-1, toupper('0xb8') gives " << std::toupper(c) << '\n'; std::setlocale(LC_ALL, "en_US.iso885915"); std::cout << "in iso8859-15, toupper('0xb8') gives " << std::toupper(c) << '\n'; }
出力
aA bB cC dD eE fF gG hH iI jJ kK lL mM
nN oO pP qQ rR sS tT uU vV wW xX yY zZ
in iso8859-1, toupper('0xb8') gives 0xb8
in iso8859-15, toupper('0xb8') gives 0xb4[編集] 関連項目
| 文字を小文字に変換する (関数) | |
| ロケールのctypeファセットを使用して文字を大文字に変換する (関数テンプレート) | |
| ワイド文字を大文字に変換する (関数) | |
| Cドキュメント for toupper
| |
[編集] 外部リンク
| 1. | ISO/IEC 8859-1。Wikipediaより。 |
| 2. | ISO/IEC 8859-15。Wikipediaより。 |