std::ctype<char>

From Cppreference

Jump to: navigation, search
Defined in header <locale>

template<> class ctype<char>;

This specialization of std::ctype encapsulates character classification features for type char. Unlike general-purpose std::ctype, table lookup is used to classify characters. The base class std::ctype<char> implements character classification equivalent to the "C" locale. The classification rules can be extended or modified 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.

Contents

[edit] Member types

Member type Definition
char_type char

[edit] Member objects

Member name Type
id (static) std::locale::id
table_size (static const) std::size_t size of the classification table, at least 256

[edit] Member functions

(constructor)
constructs a new std::ctype<char> facet
(public member function)
(destructor)
destructs a std::ctype<char> facet
(protected member function)
table
obtains the character classification table
(public member function)
classic_table [static]
obtains the "C" locale character classification table
(public static member function)
table-driven character classification functions
is
classifies a character or a character sequence
(public member function)
scan_is
locates the first character in a sequence that conforms to given classification
(public member function)
scan_not
locates the first character in a sequence that fails given classification
(public member function)
polymorphic character transformation functions
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)
the following protected member functions can be overridden by a user-defined facet derived from ctype<char>
do_toupper [virtual]
converts a character or characters to uppercase
(virtual protected member function)
do_tolower [virtual]
converts a character or characters to lowercase
(virtual protected member function)
do_widen [virtual]
identity conversion (from char to char) unless overridden
(virtual protected member function)
do_narrow [virtual]
identity conversion (from char to char) unless overridden
(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
(public static member constant)
print the value of mask identifying printable character classification
(public static member constant)
cntrl the value of mask identifying control character classification
(public static member constant)
upper the value of mask identifying uppercase character classification
(public static member constant)
lower the value of mask identifying lowercase character classification
(public static member constant)
alpha the value of mask identifying alphabetic character classification
(public static member constant)
digit the value of mask identifying digit character classification
(public static member constant)
punct the value of mask identifying punctuation character classification
(public static member constant)
xdigit the value of mask identifying hexadecimal digit character classification
(public static member constant)
blank (C++11) the value of mask identifying blank character classification
(public static member constant)
alnum alpha | digit
(public static member constant)
graph alnum | punct
(public static member constant)

[edit] Example

The following example demonstrates modification of ctype<char> to tokenize of a CSV file

#include <iostream>
#include <vector>
#include <locale>
#include <sstream>
 
// 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] See also

ctype
defines character classification tables
(class template)
ctype_base
defines character classification categories
(class template)
ctype_byname
creates a ctype facet for the named locale
(class template)