Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::char_traits

      From cppreference.com
      <cpp‎ |string
       
       
       
       
      Defined in header<string>
      template<

         class CharT

      >class char_traits;

      Thechar_traits class is a traits class template that abstracts basic character and string operations for a given character type. The defined operation set is such that generic algorithms almost always can be implemented in terms of it. It is thus possible to use such algorithms with almost any possible character or string type, just by supplying a customizedchar_traits class.

      Thechar_traits class template serves as a basis for explicit instantiations. The user canprovide a specialization for any custom character types. Several explicit specializations are provided for the standard character types (see below), other specializations are not required to satisfy the requirements ofCharTraits.

      Contents

      [edit]Specializations

      The standard library provides the following standard specializations:

      Defined in header<string>
      std::char_traits<char> the standard character traits ofchar
      std::char_traits<wchar_t> the standard character traits ofwchar_t
      std::char_traits<char8_t>(C++20) the standard character traits ofchar8_t
      std::char_traits<char16_t>(C++11) the standard character traits ofchar16_t
      std::char_traits<char32_t>(C++11) the standard character traits ofchar32_t

      All these specializations satisfy the requirements ofCharTraits.

      [edit]Member types

      The standard specializations define the following member types required byCharTraits:

      CharTMember type
       char_type int_typeoff_typepos_typestate_type
      charcharint std::streamoff std::streampos std::mbstate_t 
      wchar_twchar_tstd::wint_tstd::wstreampos
      char8_tchar8_tunsignedintstd::u8streampos
       char16_t char16_t std::uint_least16_t  std::u16streampos 
      char32_tchar32_tstd::uint_least32_tstd::u32streampos

      On top of that, the standard specializations also define the member typecomparison_category asstd::strong_ordering.

      (since C++20)

      [edit]Member functions

      The standard specializations define the following static member functions required byCharTraits:

      [static]
      assigns a character
      (public static member function)[edit]
      [static]
      compares two characters
      (public static member function)[edit]
      [static]
      moves one character sequence onto another
      (public static member function)[edit]
      [static]
      copies a character sequence
      (public static member function)[edit]
      [static]
      lexicographically compares two character sequences
      (public static member function)[edit]
      [static]
      returns the length of a character sequence
      (public static member function)[edit]
      [static]
      finds a character in a character sequence
      (public static member function)[edit]
      [static]
      convertsint_type to equivalentchar_type
      (public static member function)[edit]
      [static]
      convertschar_type to equivalentint_type
      (public static member function)[edit]
      [static]
      compares twoint_type values
      (public static member function)[edit]
      [static]
      returns aneof value
      (public static member function)[edit]
      [static]
      checks whether a character iseof value
      (public static member function)[edit]

      [edit]Notes

      CharTraits does not require defining the types and functions listed above as direct members, it only requires types likeX::type and expressions likeX::func(args) are valid and have the required semantics. Users-defined character traits can be derived from other character traits classes and only override some of their members, see the example below.

      [edit]Example

      User-defined character traits may be used to providecase-insensitive comparison:

      Run this code
      #include <cctype>#include <iostream>#include <string>#include <string_view> struct ci_char_traits:public std::char_traits<char>{staticchar to_upper(char ch){returnstd::toupper((unsignedchar) ch);} staticbool eq(char c1,char c2){return to_upper(c1)== to_upper(c2);} staticbool lt(char c1,char c2){return to_upper(c1)< to_upper(c2);} staticint compare(constchar* s1,constchar* s2,std::size_t n){while(n--!=0){if(to_upper(*s1)< to_upper(*s2))return-1;if(to_upper(*s1)> to_upper(*s2))return1;++s1;++s2;}return0;} staticconstchar* find(constchar* s,std::size_t n,char a){constauto ua{to_upper(a)};while(n--!=0){if(to_upper(*s)== ua)return s;            s++;}return nullptr;}}; template<class DstTraits,class CharT,class SrcTraits>constexprstd::basic_string_view<CharT, DstTraits>    traits_cast(conststd::basic_string_view<CharT, SrcTraits> src)noexcept{return{src.data(), src.size()};} int main(){usingnamespace std::literals; constexprauto s1="Hello"sv;constexprauto s2="heLLo"sv; if(traits_cast<ci_char_traits>(s1)== traits_cast<ci_char_traits>(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)[edit]
      read-only string view
      (class template)[edit]
      wraps a given abstract device (std::basic_streambuf)
      and provides high-level input interface
      (class template)[edit]
      wraps a given abstract device (std::basic_streambuf)
      and provides high-level output interface
      (class template)[edit]
      abstracts a raw device
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/char_traits&oldid=158926"

      [8]ページ先頭

      ©2009-2025 Movatter.jp