在线文档教程
C++
字符串 | Strings

std::char_traits

STD::CHAR[医]性状

Defined in header
template< class CharT > class char_traits;

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

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

Instantiationchar_typeint_typeoff_typepos_typestate_type
char_traits<char>charintstd::streamoffstd::streamposstd::mbstate_t
char_traits<wchar_t>wchar_tstd::wint_tstd::wstreamoffstd::wstreamposstd::mbstate_t
char_traits<char16_t> (C++11)char16_tstd::uint_least16_tstd::streamoffstd::u16streamposstd::mbstate_t
char_traits<char32_t> (C++11)char32_tstd::uint_least32_tstd::streamoffstd::u32streamposstd::mbstate_t

char_traits类模板满足CharTraits...

成员类型

TypeDefinition
char_typeCharT
int_typean integer type that can hold all values of char_type plus EOF
off_typeimplementation-defined
pos_typeimplementation-defined
state_typeimplementation-defined

成员函数

assign staticassigns a character (public static member function)
eqlt staticcompares two characters (public static member function)
move staticmoves one character sequence onto another (public static member function)
copy staticcopies a character sequence (public static member function)
compare staticlexicographically compares two character sequences (public static member function)
length staticreturns the length of a character sequence (public static member function)
find staticfinds a character in a character sequence (public static member function)
to_char_type staticconverts int_type to equivalent char_type (public static member function)
to_int_type staticconverts char_type to equivalent int_type (public static member function)
eq_int_type staticcompares two int_type values (public static member function)
eof staticreturns an eof value (public static member function)
not_eof staticchecks whether a character is eof value (public static member function)

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

二次

#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"; }

二次

产出:

二次

Hello and heLLo are equal

二次

另见

basic_stringstores and manipulates sequences of characters (class template)

© cppreference.com

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

http://en.cppreference.com/w/cpp/string/char[医]性状