Namespaces
Variants
Views
Actions

std::char_traits

From cppreference.com
< cpp‎ | string
Revision as of 00:09, 8 June 2013 by P12 (Talk | contribs)

Defined in header <string>
template<

    class CharT

> class char_traits;

The char_traits class is a traits class template that abstracts basic character and string operations for a given character type. The defined operation set is such generic algorithms almost always can be implemented in terms of it. It is thus possible to use such algorithms with almost any possible character or string type, just by supplying customized char_traits class.

The char_traits class template serves as a basis for explicit instantiations. The user can provide a specialization for any custom character types. Several specializations are defined for the standard character types. The values of the member typedefs are as follows.

Instantiation char_type int_type off_type pos_type state_type
char_traits<char> char int std::streamoff std::streampos std::mbstate_t
char_traits<wchar_t> wchar_t std::wint_t std::wstreamoff std::wstreampos std::mbstate_t
char_traits<char16_t> (C++11) char16_t std::int_least16_t std::streamoff std::u16streampos std::mbstate_t
char_traits<char32_t> (C++11) char32_t std::int_least32_t std::streamoff std::u32streampos std::mbstate_t

The char_traits class template fulfills all requirements of Traits concept.

Contents

Member types

Type Definition
char_type CharT
int_type an integer type that can hold all values of char_type plus EOF
off_type implementation-defined
pos_type implementation-defined
state_type implementation-defined

Member functions

[static]
assigns a character
(public static member function) [edit]
[static]
compares two characters
(public static member function) [edit]
[static]
moves one character sequence onto another
(public static member function) [edit]
[static]
copies a character sequence
(public static member function) [edit]
[static]
lexicographically compares two character sequences
(public static member function) [edit]
[static]
returns the length of a character sequence
(public static member function) [edit]
[static]
finds a character in a character sequence
(public static member function) [edit]
converts int_type to equivalent char_type
(public static member function) [edit]
[static]
converts char_type to equivalent int_type
(public static member function) [edit]
[static]
compares two int_type values
(public static member function) [edit]
[static]
returns an eof value
(public static member function) [edit]
[static]
checks whether a character is eof value
(public static member function) [edit]

Example

User-defined character traits may be used to provide case-insensitive comparison

#include <string>
#include <iostream>
#include <cctype>
 
struct ci_char_traits : public std::char_traits<char> {
    static bool eq(char c1, char c2) {
         return std::toupper(c1) == std::toupper(c2);
     }
    static bool ne(char c1, char c2) {
         return std::toupper(c1) != std::toupper(c2);
     }
    static bool lt(char c1, char c2) {
         return std::toupper(c1) <  std::toupper(c2);
    }
    static int compare(const char* s1, const char* s2, size_t n) {
        while( n-- != 0 ) {
            if( std::toupper(*s1) < std::toupper(*s2) ) return -1;
            if( std::toupper(*s1) > std::toupper(*s2) ) return 1;
            ++s1; ++s2;
        }
        return 0;
    }
    static const char* find(const char* s, int n, char a) {
        while( n-- > 0 && std::toupper(*s) != std::toupper(a) ) {
            ++s;
        }
        return s;
    }
};
 
typedef std::basic_string<char, ci_char_traits> ci_string;
 
std::ostream& operator<<(std::ostream& os, const ci_string& str) {
    return os.write(str.data(), str.size());
}
 
int main()
{
    ci_string s1 = "Hello";
    ci_string s2 = "heLLo";
    if(s1 == s2)
        std::cout << s1 << " and " << s2 << " are equal\n";
}

Output:

Hello and heLLo are equal

See also

stores and manipulates sequences of characters
(class template) [edit]