Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ctype<char>

      From cppreference.com
      <cpp‎ |locale
       
       
       
      Localization library
       
      Defined in header<locale>
      template<>
      class ctype<char>;

      This specialization ofstd::ctype encapsulates character classification features for typechar. Unlike general-purposestd::ctype, which uses virtual functions, this specialization uses table lookup to classify characters (which is generally faster).

      The base classstd::ctype<char> implements character classification equivalent to the minimal "C" locale. The classification rules can be extended or modified if constructed with a non-default classification table argument, if constructed asstd::ctype_byname<char> or as a user-defined derived facet. Allstd::istream formatted input functions are required to usestd::ctype<char> for character classing during input parsing.

      std-ctype char-inheritance.svg

      Contents

      [edit]Nested types

      Type Definition
      char_typechar

      [edit]Data members

      Member Description
      std::locale::idid[static] the identifier of thefacet
      conststd::size_ttable_size[static] size of the classification table, at least 256

      [edit]Member functions

      constructs a newctype<char> facet
      (public member function)[edit]
      destructs actype<char> facet
      (protected member function)[edit]
      obtains the character classification table
      (public member function)[edit]
      obtains the "C" locale character classification table
      (public static member function)[edit]
      classifies a character or a character sequence, using the classification table
      (public member function)[edit]
      locates the first character in a sequence that conforms to given classification, using the classification table
      (public member function)[edit]
      locates the first character in a sequence that fails given classification, using the classification table
      (public member function)[edit]
      invokesdo_toupper
      (public member function ofstd::ctype<CharT>)[edit]
      invokesdo_tolower
      (public member function ofstd::ctype<CharT>)[edit]
      invokesdo_widen
      (public member function ofstd::ctype<CharT>)[edit]
      invokesdo_narrow
      (public member function ofstd::ctype<CharT>)[edit]

      [edit]Protected member functions

      [virtual]
      converts a character or characters to uppercase
      (virtual protected member function ofstd::ctype<CharT>)[edit]
      [virtual]
      converts a character or characters to lowercase
      (virtual protected member function ofstd::ctype<CharT>)[edit]
      [virtual]
      converts a character or characters fromchar toCharT
      (virtual protected member function ofstd::ctype<CharT>)[edit]
      [virtual]
      converts a character or characters fromCharT tochar
      (virtual protected member function ofstd::ctype<CharT>)[edit]

      Inherited fromstd::ctype_base

      Nested types

      Type Definition
      mask unspecifiedBitmaskType type (enumeration, integer type, or bitset)

      Member constants

      space
      [static]
      the value ofmask identifying whitespace character classification
      (public static member constant)
      print
      [static]
      the value ofmask identifying printable character classification
      (public static member constant)
      cntrl
      [static]
      the value ofmask identifying control character classification
      (public static member constant)
      upper
      [static]
      the value ofmask identifying uppercase character classification
      (public static member constant)
      lower
      [static]
      the value ofmask identifying lowercase character classification
      (public static member constant)
      alpha
      [static]
      the value ofmask identifying alphabetic character classification
      (public static member constant)
      digit
      [static]
      the value ofmask identifying digit character classification
      (public static member constant)
      punct
      [static]
      the value ofmask identifying punctuation character classification
      (public static member constant)
      xdigit
      [static]
      the value ofmask identifying hexadecimal digit character classification
      (public static member constant)
      blank
      [static](C++11)
      the value ofmask identifying blank character classification
      (public static member constant)
      alnum
      [static]
      alpha| digit
      (public static member constant)
      graph
      [static]
      alnum| punct
      (public static member constant)

      [edit]Example

      The following example demonstrates modification ofctype<char> to tokenize comma-separated values:

      Run this code
      #include <cstddef>#include <iostream>#include <locale>#include <sstream>#include <vector> // This ctype facet classifies commas and endlines as whitespacestruct csv_whitespace:std::ctype<char>{staticconst mask* make_table(){// make a copy of the "C" locale tablestaticstd::vector<mask> v(classic_table(), classic_table()+ table_size);        v[',']|=  space;// comma will be classified as whitespace        v[' ']&= ~space;// space will not be classified as whitespacereturn&v[0];}     csv_whitespace(std::size_t refs=0): ctype(make_table(),false, refs){}}; int main(){std::string in="Column 1,Column 2,Column 3\n123,456,789";std::string token; std::cout<<"Default locale:\n";std::istringstream s1(in);while(s1>> token)std::cout<<"  "<< token<<'\n'; std::cout<<"Locale with modified ctype:\n";std::istringstream s2(in);    s2.imbue(std::locale(s2.getloc(), new csv_whitespace));while(s2>> token)std::cout<<"  "<< token<<'\n';}

      Output:

      Default locale:  Column  1,Column  2,Column  3  123,456,789Locale with modified ctype:  Column 1  Column 2  Column 3  123  456  789

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 695C++98table() andclassic_table() were protected member functionsmade them public

      [edit]See also

      defines character classification tables
      (class template)[edit]
      defines character classification categories
      (class)[edit]
      represents the system-suppliedstd::ctype for the named locale
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/ctype_char&oldid=177713"

      [8]ページ先頭

      ©2009-2025 Movatter.jp