Namespaces
Variants
Views
Actions

std::ctype<char>

From cppreference.com
< cpp‎ | locale
Revision as of 19:19, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <locale>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td class="t-dcl-nopad">
template<>
class ctype<char>;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

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.

cpp/locale/ctype basecpp/locale/locale/facetstd-ctype char-inheritance.svg
About this image

Inheritance diagram

Contents

Member types

Member type Definition
char_type char

Member functions

Template:cpp/locale/ctype char/dcl list constructorTemplate:cpp/locale/ctype char/dcl list destructorTemplate:cpp/locale/ctype char/dcl list tableTemplate:cpp/locale/ctype char/dcl list classic tableTemplate:cpp/locale/ctype char/dcl list isTemplate:cpp/locale/ctype char/dcl list scan isTemplate:cpp/locale/ctype char/dcl list scan notTemplate:cpp/locale/ctype/dcl list toupperTemplate:cpp/locale/ctype/dcl list tolowerTemplate:cpp/locale/ctype/dcl list widenTemplate:cpp/locale/ctype/dcl list narrow

Protected member functions

Template:cpp/locale/ctype/dcl list do toupperTemplate:cpp/locale/ctype/dcl list do tolowerTemplate:cpp/locale/ctype/dcl list do widenTemplate:cpp/locale/ctype/dcl list do narrow

Member objects

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

Inherited from std::ctype_base

Member types

Type Definition
mask unspecified bitmask 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)

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

See also

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