std::char_traits
From cppreference.com
Defined in header <string>
|
||
template<
class CharT |
||
The char_traits
class defines the stream and string operation properties of a character type, such as the types used for manipulating the characters and character strings, as well as all the common operations for the given character type.
There is class template char_traits
defined, which serves as a basis for explicit instantiations. It fulfills all requirements of Traits
concept.
Also, several specializations are defined for most common character types which which has to specify the following members:
Instantiation | char_type
|
int_type
|
off_type
|
pos_type
|
state_type
|
---|---|---|---|---|---|
char_traits<char> | char | int | streamoff | streampos | mbstate_t |
char_traits<wchar_t> | wchar_t | wint_t | wstreamoff | wstreampos | mbstate_t |
char_traits<char16_t> (C++11) | char16_t | int_least16_t | streamoff | u16streampos | mbstate_t |
char_traits<char32_t> (C++11) | char32_t | int_least32_t | streamoff | u32streampos | mbstate_t |
This section is incomplete Reason: simplify the description, emphasize that char_traits can be user defined |
Contents |
[edit] Member types
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 |
[edit] Member functions
[static]
|
assigns a character (public static member function) |
[static]
|
compares two characters (public static member function) |
[static]
|
moves one character sequence onto another (public static member function) |
[static]
|
copies a character sequence (public static member function) |
[static]
|
lexicographically compares two character sequences (public static member function) |
[static]
|
returns the length of a character sequence (public static member function) |
[static]
|
finds a character in a character sequence (public static member function) |
[static]
|
converts int_type to equivalent char_type (public static member function) |
[static]
|
converts char_type to equivalent int_type (public static member function) |
[static]
|
compares two int_type values (public static member function) |
[static]
|
returns an eof value (public static member function) |
[static]
|
checks whether a character is eof value (public static member function) |
[edit] Example
User-defined character traits may be used to provide case-insensitive comparison
#include <string> #include <iostream> #include <cctype> struct ci_char_traits : public std::char_traits<char> { static bool eq(char c1, char c2) { return std::toupper(c1) == std::toupper(c2); } static bool ne(char c1, char c2) { return std::toupper(c1) != std::toupper(c2); } static bool lt(char c1, char c2) { return std::toupper(c1) < std::toupper(c2); } static int compare(const char* s1, const char* s2, size_t n) { while( n-- != 0 ) { if( std::toupper(*s1) < std::toupper(*s2) ) return -1; if( std::toupper(*s1) > std::toupper(*s2) ) return 1; ++s1; ++s2; } return 0; } static const char* find(const char* s, int n, char a) { while( n-- > 0 && std::toupper(*s) != std::toupper(a) ) { ++s; } return s; } }; typedef std::basic_string<char, ci_char_traits> ci_string; 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"; }
Output:
Hello and heLLo are equal
[edit] See also
stores and manipulates sequences of characters (class template) |