名稱空間
變體
操作

std::char_traits

來自 cppreference.com
< cpp‎ | string
定義於標頭檔案 <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 所需的以下靜態成員函式

[靜態]
賦值一個字元
(public static member function) [編輯]
[靜態]
比較兩個字元
(public static member function) [編輯]
[靜態]
將一個字元序列移動到另一個
(public static member function) [編輯]
[靜態]
複製一個字元序列
(public static member function) [編輯]
[靜態]
按字典序比較兩個字元序列
(public static member function) [編輯]
[靜態]
返回字元序列的長度
(public static member function) [編輯]
[靜態]
在字元序列中查詢字元
(public static member function) [編輯]
int_type 轉換為等價的 char_type
(public static member function) [編輯]
[靜態]
char_type 轉換為等價的 int_type
(public static member function) [編輯]
[靜態]
比較兩個 int_type
(public static member function) [編輯]
[靜態]
返回一個 eof
(public static member function) [編輯]
[靜態]
檢查字元是否為 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
並提供高階輸出介面
(類模板) [編輯]
抽象原始裝置
(類模板) [編輯]