Namespaces
Variants

std:: char_traits

From cppreference.net
ヘッダーで定義 <string>
template <

class CharT

> class char_traits ;

char_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

さらに、標準特殊化はメンバ型 comparison_category std::strong_ordering として定義する。

(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)
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

関連項目

文字シーケンスを格納および操作する
(クラステンプレート)
読み取り専用文字列ビュー
(クラステンプレート)
指定された抽象デバイス ( std::basic_streambuf ) をラップし、
高レベル入力インターフェースを提供する
(クラステンプレート)
指定された抽象デバイス ( std::basic_streambuf ) をラップし、
高レベル出力インターフェースを提供する
(クラステンプレート)
生デバイスを抽象化する
(クラステンプレート)