< cpp‎ | string
Revision as of 11:56, 23 April 2013 by Eendy (Talk | contribs)

Defined in header <string>

    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.


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

Template:cpp/string/char traits/dcl list assignTemplate:cpp/string/char traits/dcl list cmpTemplate:cpp/string/char traits/dcl list moveTemplate:cpp/string/char traits/dcl list copyTemplate:cpp/string/char traits/dcl list compareTemplate:cpp/string/char traits/dcl list lengthTemplate:cpp/string/char traits/dcl list findTemplate:cpp/string/char traits/dcl list to char typeTemplate:cpp/string/char traits/dcl list to int typeTemplate:cpp/string/char traits/dcl list eq int typeTemplate:cpp/string/char traits/dcl list eofTemplate:cpp/string/char traits/dcl list not eof


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) ) {
        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.size());
int main()
    ci_string s1 = "Hello";
    ci_string s2 = "heLLo";
    if(s1 == s2)
        std::cout << s1 << " and " << s2 << " are equal\n";


Hello and heLLo are equal

See also

Template:cpp/string/dcl list basic string