std::char_traits
cppreference.com
<tbody>
</tbody>
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:
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 | ||
|
, |
( C++20) |
-
-, CharTraits:
[static] |
(public static -) |
[static] |
(public static -) |
[static] |
(public static -) |
[static] |
(public static -) |
[static] |
(public static -) |
[static] |
(public static -) |
[static] |
(public static -) |
[static] |
int_type char_type (public static -) |
[static] |
char_type int_type (public static -) |
[static] |
int_type (public static -) |
[static] |
eof (public static -) |
[static] |
, eof (public static -) |
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 << " " << s2 << " \n";
}
:
Hello heLLo
| ( ) | |
(C++17) |
( ) |
| (std::basic_streambuf) ( ) | |
| (std::basic_streambuf) ( ) | |
| ( ) |