SysTutorials

Linux Manual Pages › Section 3

stdctype(3)

std::ctype<char> – std::ctype<char>

Synopsis

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, 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.
 std-ctype char-inheritance.svg
Inheritance diagram

Member types

Member type Definition
char_type char

Member functions

              constructs a new std::ctype<char> facet
constructor (public member function)
              destructs a std::ctype<char> facet
destructor (protected member function)
              obtains the character classification table
table (public member function)

classic_table obtains the "C" locale character classification table
              (public static member function)
[static]
              classifies a character or a character sequence, using the classification table
is (public member function)
              locates the first character in a sequence that conforms to given classification, using the classification table
scan_is (public member function)
              locates the first character in a sequence that fails given classification, using the classification table
scan_not (public member function)
              invokes do_toupper
toupper (public member function of std::ctype<CharT>)
              invokes do_tolower
tolower (public member function of std::ctype<CharT>)
              invokes do_widen
widen (public member function of std::ctype<CharT>)
              invokes do_narrow
narrow (public member function of std::ctype<CharT>)

Protected member functions

do_toupper converts a character or characters to uppercase
           (virtual protected member function of std::ctype<CharT>)
[virtual]

do_tolower converts a character or characters to lowercase
           (virtual protected member function of std::ctype<CharT>)
[virtual]

do_widen converts a character or characters from char to charT
           (virtual protected member function of std::ctype<CharT>)
[virtual]

do_narrow converts a character or characters from charT to char
           (virtual protected member function of std::ctype<CharT>)
[virtual]

Member objects

static std::locale::id id id of the locale
                                     (public static member constant)
[static]

static const std::size_t table_size size of the classification table, at least 256
                                     (public static member constant)
[static]

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 ctype<char> to tokenize comma-separated values
// Run this code

#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
123,456,789";
    std::string token;
    std::cout << "default locale:
";
    std::istringstream s1(in);
    while (s1 >> token)
        std::cout << "  " << token << '
';
    std::cout << "locale with modified ctype:
";
    std::istringstream s2(in);
    s2.imbue(std::locale(s2.getloc(), new csv_whitespace));
    while (s2 >> token)
        std::cout << "  " << token << '
';
}

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

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