Scroll to navigation

std::ctype(3) C++ Standard Libary std::ctype(3)

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

Specializations


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


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

Member types


Member type Definition
char_type CharT

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)
scan_is invokes do_scan_is
(public member function)
scan_not invokes do_scan_not
(public member function)
toupper invokes do_toupper
(public member function)
tolower invokes do_tolower
(public member function)
widen invokes do_widen
(public member function)
narrow invokes do_narrow
(public member function)

Member objects


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

Protected member functions


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

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
[static] (public static member constant)
print the value of mask identifying printable character classification
[static] (public static member constant)
cntrl the value of mask identifying control character classification
[static] (public static member constant)
upper the value of mask identifying uppercase character classification
[static] (public static member constant)
lower the value of mask identifying lowercase character classification
[static] (public static member constant)
alpha the value of mask identifying alphabetic character classification
[static] (public static member constant)
digit the value of mask identifying digit character classification
[static] (public static member constant)
punct the value of mask identifying punctuation character classification
[static] (public static member constant)
xdigit the value of mask identifying hexadecimal digit character
[static] classification
(public static member constant)
blank the value of mask identifying blank character classification
[static] (C++11) (public static member constant)
alnum alpha | digit
[static] (public static member constant)
graph alnum | punct
[static] (public static member constant)

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

See also


ctype<char> specialization of std::ctype for type char
(class template specialization)
ctype_base defines character classification categories
(class)
ctype_byname represents the system-supplied std::ctype for the named locale
(class template)

2024.06.10 http://cppreference.com