首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::char_traits

Defined in header <string>

template< class CharT > class char_traits;

char_traits类是一个特征类模板,它抽象给定字符类型的基本字符和字符串操作。定义的操作集使得泛型算法几乎总是可以根据它来实现。因此,只需提供自定义的,就可以在几乎任何可能的字符或字符串类型中使用这些算法。char_traits上课。

char_traits类模板是显式实例化的基础。用户可以提供专业化用于任何自定义字符类型。为标准字符类型定义了几个专门化。成员类型的值如下。

Instantiation

char_type

int_type

off_type

pos_type

state_type

char_traits<char>

char

int

std::streamoff

std::streampos

std::mbstate_t

char_traits<wchar_t>

wchar_t

std::wint_t

std::wstreamoff

std::wstreampos

std::mbstate_t

char_traits<char16_t> (C++11)

char16_t

std::uint_least16_t

std::streamoff

std::u16streampos

std::mbstate_t

char_traits<char32_t> (C++11)

char32_t

std::uint_least32_t

std::streamoff

std::u32streampos

std::mbstate_t

char_traits类模板满足CharTraits...

成员类型

Type

Definition

char_type

CharT

int_type

an integer type that can hold all values of char_type plus EOF

off_type

implementation-defined

pos_type

implementation-defined

state_type

implementation-defined

成员函数

assign static

assigns a character (public static member function)

eqlt static

compares two characters (public static member function)

move static

moves one character sequence onto another (public static member function)

copy static

copies a character sequence (public static member function)

compare static

lexicographically compares two character sequences (public static member function)

length static

returns the length of a character sequence (public static member function)

find static

finds a character in a character sequence (public static member function)

to_char_type static

converts int_type to equivalent char_type (public static member function)

to_int_type static

converts char_type to equivalent int_type (public static member function)

eq_int_type static

compares two int_type values (public static member function)

eof static

returns an eof value (public static member function)

not_eof static

checks whether a character is eof value (public static member function)

用户定义的字符特征可用于提供不区分大小写比较...

二次

代码语言:javascript
复制
#include <string>
#include <iostream>
#include <cctype>
 
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, 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, int n, char a) {
        auto const ua (to_upper(a));
        while ( n-- != 0 ) 
        {
            if (to_upper(*s) == ua)
                return s;
            s++;
        }
        return nullptr;
    }
};
 
using ci_string = std::basic_string<char, ci_char_traits>;
 
std::ostream& operator<<(std::ostream& os, const ci_string& str) {
    return os.write(str.data(), str.size());
}
 
int main()
{
    ci_string s1 = "Hello";
    ci_string s2 = "heLLo";
    if (s1 == s2)
        std::cout << s1 << " and " << s2 << " are equal\n";
}

二次

产出:

二次

代码语言:javascript
复制
Hello and heLLo are equal

二次

另见

basic_string

stores and manipulates sequences of characters (class template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券