std::ctype - cppreference.com (original) (raw)

| | | | | ------------------------------------- | | | | 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

Contents

[edit] Specializations

The standard library is guaranteed to provide the following specializations (they are required to be implemented by any locale object):

std::ctype 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

[edit] Nested types

Type Definition
char_type CharT

[edit] Data members

[edit] Member functions

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

[edit] Protected member functions

do_is[virtual] classifies a character or a character sequence (virtual protected member function) [edit]
do_scan_is[virtual] locates the first character in a sequence that conforms to given classification (virtual protected member function) [edit]
do_scan_not[virtual] locates the first character in a sequence that fails given classification (virtual protected member function) [edit]
do_toupper[virtual] converts a character or characters to uppercase (virtual protected member function) [edit]
do_tolower[virtual] converts a character or characters to lowercase (virtual protected member function) [edit]
do_widen[virtual] converts a character or characters from char to CharT (virtual protected member function) [edit]
do_narrow[virtual] converts a character or characters from CharT to char (virtual protected member function) [edit]

Inherited from std::ctype_base

Nested types

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

Member constants

space[static] the value of mask identifying whitespace character classification (public static member constant)
print[static] the value of mask identifying printable character classification (public static member constant)
cntrl[static] the value of mask identifying control character classification (public static member constant)
upper[static] the value of mask identifying uppercase character classification (public static member constant)
lower[static] the value of mask identifying lowercase character classification (public static member constant)
alpha[static] the value of mask identifying alphabetic character classification (public static member constant)
digit[static] the value of mask identifying digit character classification (public static member constant)
punct[static] the value of mask identifying punctuation character classification (public static member constant)
xdigit[static] the value of mask identifying hexadecimal digit character classification (public static member constant)
blank[static] (C++11) the value of mask 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 of a ctype other than ctype<char> to tokenize a CSV file:

#include #include #include   struct csv_whitespace : std::ctype { 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 base 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

[edit] See also

| | specialization of std::ctype for type char (class template specialization) [edit] | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | defines character classification categories (class) [edit] | | | represents the system-supplied std::ctype for the named locale (class template) [edit] |