< cpp‎ | string
Revision as of 08:33, 5 December 2012 by Cubbi (Talk | contribs)

Defined in header <string>

    class CharT

> class char_traits;

The char_traits class defines the stream and string operation properties of a character type, such as the types used for manipulating the characters and character strings, as well as all the common operations for the given character type.

There is class template char_traits defined, which serves as a basis for explicit instantiations. It fulfills all requirements of Traits concept.

Also, several specializations are defined for most common character types which which has to specify the following members:

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


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