Namespaces
Variants
Views
Actions

std::ctype

From cppreference.com
< cpp‎ | locale
Revision as of 11:01, 21 January 2013 by Cubbi (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 CharT >
class ctype;
</td>

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

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.

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

Inheritance diagram

Two specializations are provided by the standard library

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

Contents

Member types

Member type Definition
char_type CharT

Member functions

Template:cpp/locale/ctype/dcl list isTemplate:cpp/locale/ctype/dcl list scan isTemplate:cpp/locale/ctype/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
constructs a new ctype facet
(public member function)
destructs a ctype facet
(protected member function)

Protected member functions

Template:cpp/locale/ctype/dcl list do isTemplate:cpp/locale/ctype/dcl list do scan isTemplate:cpp/locale/ctype/dcl list do scan notTemplate: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
id of the locale
(public member object)

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 a ctype other than ctype<char> to tokenize of a CSV file

#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 parent 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);
    s2.imbue(std::locale(s2.getloc(), new csv_whitespace()));
    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

specialization of std::ctype for type char
(class template specialization)
defines character classification categories
(class template)
creates a ctype facet for the named locale
(class template)