std::char_traits
| ヘッダ <string> で定義 |
||
| template< class CharT |
||
char_traitsクラステンプレートは、与えられた文字型に対する基本的な文字と文字列の操作を抽象化する特性(traits)クラステンプレートです。定義されている操作セットは、汎用アルゴリズムがほとんど常にそれに基づいて実装できるように作られています。したがって、カスタマイズされたchar_traitsクラスを提供するだけで、そのようなアルゴリズムをほとんどすべての文字型や文字列型で使用することが可能になります。
char_traitsクラステンプレートは、明示的なインスタンス化の基礎として機能します。ユーザーは、任意のカスタム文字型に対して特殊化を提供できます。標準の文字型に対してはいくつかの明示的な特殊化が提供されています(下記参照)。その他の特殊化はCharTraitsの要件を満たす必要はありません。
目次 |
[編集] 特殊化
標準ライブラリは、以下の標準特殊化を提供します。
| ヘッダ
<string> で定義 | |
| std::char_traits<char> | char の標準文字特性 |
| std::char_traits<wchar_t> | wchar_t の標準文字特性 |
| std::char_traits<char8_t> (C++20) | char8_t の標準文字特性 |
| std::char_traits<char16_t> (C++11) | char16_t の標準文字特性 |
| std::char_traits<char32_t> (C++11) | char32_t の標準文字特性 |
これらすべての特殊化は、CharTraitsの要件を満たします。
[編集] メンバ型
標準特殊化は、CharTraitsで要求される以下のメンバ型を定義します。
CharT
|
メンバ型 | ||||
|---|---|---|---|---|---|
char_type
|
int_type
|
off_type
|
pos_type
|
state_type
| |
| char | char | int | std::streamoff | std::streampos | std::mbstate_t |
| wchar_t | wchar_t | std::wint_t | std::wstreampos | ||
| char8_t | char8_t | unsigned int | std::u8streampos | ||
| char16_t | char16_t | std::uint_least16_t | std::u16streampos | ||
| char32_t | char32_t | std::uint_least32_t | std::u32streampos | ||
|
それに加えて、標準特殊化はメンバ型 |
(C++20以降) |
[編集] メンバ関数
標準特殊化は、CharTraitsで要求される以下の静的メンバ関数を定義します。
| [static] |
文字を代入する (public static member function) |
| [static] |
2つの文字を比較する (public static member function) |
| [static] |
ある文字シーケンスを別のシーケンス上に移動する (public static member function) |
| [static] |
文字シーケンスをコピーする (public static member function) |
| [static] |
2つの文字シーケンスを辞書順で比較する (public static member function) |
| [static] |
文字シーケンスの長さを返す (public static member function) |
| [static] |
文字シーケンスの中から文字を検索する (public static member function) |
| [static] |
int_typeを等価なchar_typeに変換する(public static member function) |
| [static] |
char_typeを等価なint_typeに変換する(public static member function) |
| [static] |
2つのint_typeの値を比較する(public static member function) |
| [static] |
eof値を返す (public static member function) |
| [static] |
文字がeof値かどうかをチェックする (public static member function) |
[編集] ノート
CharTraitsは、上記の型や関数を直接のメンバとして定義することを要求しません。要求するのは、X::typeのような型やX::func(args)のような式が有効であり、要求されるセマンティクスを持つことだけです。ユーザー定義の文字特性は、他の文字特性クラスから派生させ、一部のメンバのみをオーバーライドすることができます。以下の例を参照してください。
[編集] 例
ユーザー定義の文字特性は、大文字小文字を区別しない比較を提供するために使用できます。
#include <cctype> #include <iostream> #include <string> #include <string_view> struct ci_char_traits : public std::char_traits<char> { static char to_upper(char ch) { return std::toupper((unsigned char) ch); } static bool eq(char c1, char c2) { return to_upper(c1) == to_upper(c2); } static bool lt(char c1, char c2) { return to_upper(c1) < to_upper(c2); } static int compare(const char* s1, const char* s2, std::size_t n) { while (n-- != 0) { if (to_upper(*s1) < to_upper(*s2)) return -1; if (to_upper(*s1) > to_upper(*s2)) return 1; ++s1; ++s2; } return 0; } static const char* find(const char* s, std::size_t n, char a) { const auto ua{to_upper(a)}; while (n-- != 0) { if (to_upper(*s) == ua) return s; s++; } return nullptr; } }; template<class DstTraits, class CharT, class SrcTraits> constexpr std::basic_string_view<CharT, DstTraits> traits_cast(const std::basic_string_view<CharT, SrcTraits> src) noexcept { return {src.data(), src.size()}; } int main() { using namespace std::literals; constexpr auto s1 = "Hello"sv; constexpr auto s2 = "heLLo"sv; if (traits_cast<ci_char_traits>(s1) == traits_cast<ci_char_traits>(s2)) std::cout << s1 << " and " << s2 << " are equal\n"; }
出力
Hello and heLLo are equal
[編集] 関連項目
| 文字のシーケンスを格納し操作する (クラステンプレート) | |
| (C++17) |
読み取り専用の文字列ビュー (class template) |
| 与えられた抽象デバイス(std::basic_streambuf)をラップし 高水準な入力インターフェースを提供する (クラステンプレート) | |
| 与えられた抽象デバイス(std::basic_streambuf)をラップし 高水準な出力インターフェースを提供する (クラステンプレート) | |
| 生のデバイスを抽象化する (クラステンプレート) |