template<>
class ctype<char>;
This specialization of std::ctype encapsulates character classification features for type char. Unlike general-purpose std::ctype, which uses virtual functions, this specialization uses table lookup to classify characters (which is generally faster).
The base class std::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 as std::ctype_byname<char> or as a user-defined derived facet. All std::istream formatted input functions are required to use std::ctype
<char> for character classing during input parsing.
Inheritance diagram
[edit] Nested types Type Definitionchar_type
char [edit] Data members Member Description std::locale::id id
[static] the identifier of the facet const std::size_t table_size
[static] size of the classification table, at least 256 [edit] Member functions constructs a new ctype<char> facet
do_toupper
std::ctype<CharT>
) [edit] invokes do_tolower
std::ctype<CharT>
) [edit] invokes do_widen
std::ctype<CharT>
) [edit] invokes do_narrow
std::ctype<CharT>
) [edit] [edit] Protected member functions converts a character or characters to uppercase
std::ctype<CharT>
) [edit] converts a character or characters to lowercase
std::ctype<CharT>
) [edit] converts a character or characters from char to CharT
std::ctype<CharT>
) [edit] converts a character or characters from CharT
to char
std::ctype<CharT>
) [edit] Inherited from std::ctype_base Nested types Type Definition mask
unspecified BitmaskType type (enumeration, integer type, or bitset) Member constants the value of mask
identifying whitespace character classification
mask
identifying printable character classification
mask
identifying control character classification
mask
identifying uppercase character classification
mask
identifying lowercase character classification
mask
identifying alphabetic character classification
mask
identifying digit character classification
mask
identifying punctuation character classification
mask
identifying hexadecimal digit character classification
mask
identifying blank character classification
The following example demonstrates modification of ctype<char> to tokenize comma-separated values:
#include <cstddef> #include <iostream> #include <locale> #include <sstream> #include <vector> // This ctype facet classifies commas and endlines as whitespace struct csv_whitespace : std::ctype<char> { static const mask* make_table() { // make a copy of the "C" locale table static std::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 whitespace return &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,789 Locale 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.
DR Applied to Behavior as published Correct behavior LWG 695 C++98table()
and classic_table()
were protected member functions made them public [edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4