std::ctype (3) - Linux Manuals

std::ctype: std::ctype

NAME

std::ctype - std::ctype

Synopsis


Defined in header <locale>
template< class CharT >
class ctype;


Class ctype encapsulates character classification features. All stream input operations performed through std::basic_istream<charT> use the std::ctype<charT> of the locale imbued in the stream to identify whitespace characters for input tokenization. Stream output operations apply std::ctype<charT>::widen() to narrow-character arguments prior to output.
 std-ctype-inheritance.svg
Inheritance diagram
Two standalone (locale-independent) specializations are provided by the standard library:


Defined in header <locale>
std::ctype<char> provides narrow character equivalents of the minimal "C" locale classification. This specialization uses table lookup for character classification
std::ctype<wchar_t> provides wide character classification appropriate to the native character set


In addition, every locale object constructed in a C++ program implements its own (locale-specific) versions of these specializations.

Member types


Member type Definition
char_type CharT

Member functions


              constructs a new ctype facet
constructor (public member function)
              destructs a ctype facet
destructor (protected member function)
              invokes do_is
is (public member function)
              invokes do_scan_is
scan_is (public member function)
              invokes do_scan_not
scan_not (public member function)
              invokes do_toupper
toupper (public member function)
              invokes do_tolower
tolower (public member function)
              invokes do_widen
widen (public member function)
              invokes do_narrow
narrow (public member function)

Member objects


                          id of the locale
static std::locale::id id (public member object)

Protected member functions


do_is classifies a character or a character sequence
            (virtual protected member function)
[virtual]


do_scan_is locates the first character in a sequence that conforms to given classification
            (virtual protected member function)
[virtual]


do_scan_not locates the first character in a sequence that fails given classification
            (virtual protected member function)
[virtual]


do_toupper converts a character or characters to uppercase
            (virtual protected member function)
[virtual]


do_tolower converts a character or characters to lowercase
            (virtual protected member function)
[virtual]


do_widen converts a character or characters from char to charT
            (virtual protected member function)
[virtual]


do_narrow converts a character or characters from charT to char
            (virtual protected member function)
[virtual]


Inherited from std::ctype_base

Member types


Type Definition
mask unspecified bitmask type (enumeration, integer type, or bitset)

Member constants


space the value of mask identifying whitespace character classification
                 (public static member constant)
[static]


print the value of mask identifying printable character classification
                 (public static member constant)
[static]


cntrl the value of mask identifying control character classification
                 (public static member constant)
[static]


upper the value of mask identifying uppercase character classification
                 (public static member constant)
[static]


lower the value of mask identifying lowercase character classification
                 (public static member constant)
[static]


alpha the value of mask identifying alphabetic character classification
                 (public static member constant)
[static]


digit the value of mask identifying digit character classification
                 (public static member constant)
[static]


punct the value of mask identifying punctuation character classification
                 (public static member constant)
[static]


xdigit the value of mask identifying hexadecimal digit character classification
                 (public static member constant)
[static]


blank the value of mask identifying blank character classification
                 (public static member constant)
[static] (C++11)


alnum alpha | digit
                 (public static member constant)
[static]


graph alnum | punct
                 (public static member constant)
[static]

Example


The following example demonstrates modification of a ctype other than ctype<char> to tokenize a CSV file
// Run this code


  #include <iostream>
  #include <locale>
  #include <sstream>


  struct csv_whitespace : std::ctype<wchar_t>
  {
      bool do_is(mask m, char_type c) const
      {
          if ((m & space) && c == L' ') {
              return false; // space will NOT be classified as whitespace
          }
          if ((m & space) && c == L',') {
              return true; // comma will be classified as whitespace
          }
          return ctype::do_is(m, c); // leave the rest to the parent class
      }
  };


  int main()
  {
      std::wstring in = L"Column 1,Column 2,Column 3\n123,456,789";
      std::wstring token;


      std::wcout << "default locale:\n";
      std::wistringstream s1(in);
      while (s1 >> token) {
          std::wcout << " " << token << '\n';
      }


      std::wcout << "locale with modified ctype:\n";
      std::wistringstream s2(in);
      csv_whitespace* my_ws = new csv_whitespace;
      s2.imbue(std::locale(s2.getloc(), my_ws));
      while (s2 >> token) {
          std::wcout << " " << token<< '\n';
      }
  }

Output:


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

See also


             specialization of std::ctype for type char
ctype<char> (class template specialization)
             defines character classification categories
ctype_base (class template)
             creates a ctype facet for the named locale
ctype_byname (class template)