char_traits Struct

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

The char_traits struct describes attributes associated with a character.

template <class CharType>  
struct char_traits;  

Parameters

CharType
The element data type.

The template struct describes various character traits for type CharType. The template class basic_string as well as several iostream template classes, including basic_ios, use this information to manipulate elements of type CharType. Such an element type must not require explicit construction or destruction. It must supply a default constructor, a copy constructor, and an assignment operator, with the expected semantics. A bitwise copy must have the same effect as an assignment. None of the member functions of struct char_traits can throw exceptions.

Typedefs

char_typeA type of character.
int_typeAn integer type that can represent a character of type char_type or an end-of-file (EOF) character.
off_typeAn integer type that can represent offsets between positions in a stream.
pos_typeAn integer type that can represent positions in a stream.
state_typeA type that represents the conversion state in for multibyte characters in a stream.

Member Functions

assignAssigns one character value to another.
compareCompares up to a specified number of characters in two strings.
copyCopies a specified number of characters from one string to another. Deprecated. Use char_traits::_Copy_s instead.
_Copy_sCopies a specified number of characters from one string to another.
eofReturns the end-of-file (EOF) character.
eqTests whether two char_type characters are equal.
eq_int_typeTests whether two characters represented as int_types are equal.
findSearches for the first occurrence of a specified character in a range of characters.
lengthReturns the length of a string.
ltTests whether one character is less than another.
moveCopies a specified number of characters in a sequence to another, possible overlapping, sequence. Deprecated. Use char_traits::_Move_s instead.
_Move_sCopies a specified number of characters in a sequence to another, possible overlapping, sequence.
not_eofTests whether a character is the end-of-file (EOF) character.
to_char_typeConverts an int_type character to the corresponding char_type character and returns the result.
to_int_typeConverts a char_type character to the corresponding int_type character and returns the result.

Header: <string>

Namespace: std

Assigns one character value to another or to a range of elements in a string.

static void assign(char_type& _CharTo,
    const char_type& _CharFrom);

static char_type *assign(char_type* strTo,
    size_t _Num,
    char_type _CharFrom);

Parameters

_ CharFrom
The character whose value is to be assigned.

_CharTo
The element that is to be assigned the character value.

  • strTo*
    The string or character array whose initial elements are to be assigned character values.

_Num
The number of elements that are going to be assigned values.

Return Value

The second member function returns a pointer to the string whose first _Num elements have been assigned values of _CharFrom.

Example

// char_traits_assign.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
  
   // The first member function assigning   
   // one character value to another character  
   char ChTo = 't';  
   const char ChFrom = 'f';  
   cout << "The initial characters ( ChTo , ChFrom ) are: ( "  
        << ChTo << " , " << ChFrom << " )." << endl;  
   char_traits<char>::assign ( ChTo , ChFrom );  
   cout << "After assigning, the characters ( ChTo , ChFrom ) are: ( "  
        << ChTo << " , " << ChFrom << " )." << endl << endl;  
  
   // The second member function assigning   
   // character values to initial part of a string  
   char_traits<char>::char_type s1[] = "abcd-1234-abcd";  
   char_traits<char>::char_type* result1;  
   cout << "The target string s1 is: " << s1 << endl;  
   result1 = char_traits<char>::assign ( s1 , 4 , 'f' );  
   cout << "The result1 = assign ( s1 , 4 , 'f' ) is: "  
        << result1 << endl;  
}  

The initial characters ( ChTo , ChFrom ) are: ( t , f ).  
After assigning, the characters ( ChTo , ChFrom ) are: ( f , f ).  
  
The target string s1 is: abcd-1234-abcd  
The result1 = assign ( s1 , 4 , 'f' ) is: ffff-1234-abcd  

A type of character.

typedef CharType char_type;  

Remarks

The type is a synonym for the template parameter CharType.

Example

See the example for copy for an example of how to declare and use char_type.

Compares up to a specified number of characters in two strings.

static int compare(const char_type* str1,
    const char_type* str2,
    size_t _Num);

Parameters

  • str1*
    The first of two strings to be compared to each other.

  • str2*
    The second of two strings to be compared to each other.

_Num
The number of elements in the strings to be compared.

Return Value

A negative value if the first string is less than the second string, 0 if the two strings are equal, or a positive value if the first string is greater than the second string.

Remarks

The comparison between the strings is made element by element, first testing for equality and then, if a pair of elements in the sequence tests not equal, they are tested for less than.

If two strings compare equal over a range but one is longer than the other, then the shorter of the two is less than the longer one.

Example

// char_traits_compare.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main() {  
   using namespace std;  
  
   char_traits<char>::char_type* s1 = "CAB";  
   char_traits<char>::char_type* s2 = "ABC";  
   char_traits<char>::char_type* s3 = "ABC";  
   char_traits<char>::char_type* s4 = "ABCD";  
  
   cout << "The string s1 is: " << s1 << endl;  
   cout << "The string s2 is: " << s2 << endl;  
   cout << "The string s3 is: " << s3 << endl;  
   cout << "The string s4 is: " << s4 << endl;  
  
   int comp1, comp2, comp3, comp4;  
   comp1 = char_traits<char>::compare ( s1 , s2 , 2 );  
   comp2 = char_traits<char>::compare ( s2 , s3 , 3 );  
   comp3 = char_traits<char>::compare ( s3 , s4 , 4 );  
   comp4 = char_traits<char>::compare ( s4 , s3 , 4 );  
   cout << "compare ( s1 , s2 , 2 ) = " << comp1 << endl;  
   cout << "compare ( s2 , s3 , 3 ) = " << comp2 << endl;  
   cout << "compare ( s3 , s4 , 4 ) = " << comp3 << endl;  
   cout << "compare ( s4 , s3 , 4 ) = " << comp4 << endl;  
}  

Copies a specified number of characters from one string to another.

This method is potentially unsafe, as it relies on the caller to check that the passed values are correct. Consider using char_traits::_Copy_s instead.

static char_type *copy(char_type* _To,
    const char_type* _From,
    size_t _Num);

Parameters

_To
The element at the beginning of the string or character array targeted to receive the copied sequence of characters.

_From
The element at the beginning of the source string or character array to be copied.

_Num
The number of elements to be copied.

Return Value

The first element copied into the string or character array targeted to receive the copied sequence of characters.

Remarks

The source and destination character sequences must not overlap.

Example

// char_traits_copy.cpp  
// compile with: /EHsc /W3  
#include <string>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   char_traits<char>::char_type s1[] = "abcd-1234-abcd";  
   char_traits<char>::char_type s2[] = "ABCD-1234";  
   char_traits<char>::char_type* result1;  
   cout << "The source string is: " << s1 << endl;  
   cout << "The destination string is: " << s2 << endl;  
   // Note: char_traits::copy is potentially unsafe, consider  
   // using char_traits::_Copy_s instead.  
   result1 = char_traits<char>::copy ( s1 , s2 , 4 );  // C4996  
   cout << "The result1 = copy ( s1 , s2 , 4 ) is: "  
        << result1 << endl;  
}  

The source string is: abcd-1234-abcd  
The destination string is: ABCD-1234  
The result1 = copy ( s1 , s2 , 4 ) is: ABCD-1234-abcd  

Copies a specified number of characters from one string to another.

static char_type *_Copy_s(
    char_type* dest,  
    size_t dest_size,  
    const char_type* _From,  
    size_t count);

Parameters

dest
The string or character array targeted to receive the copied sequence of characters.

dest_size
The size of dest. If char_type is char, then this size is in bytes. If char_type is wchar_t, then this size is in words.

_From
The source string or character array to be copied.

count
The number of elements to be copied.

Return Value

The string or character array targeted to receive the copied sequence of characters.

Remarks

The source and destination character sequences must not overlap.

Example

// char_traits__Copy_s.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )  
{  
    using namespace std;  
  
    char_traits<char>::char_type s1[] = "abcd-1234-abcd";  
    char_traits<char>::char_type s2[] = "ABCD-1234";  
    char_traits<char>::char_type* result1;  
    cout << "The source string is: " << s1 << endl;  
    cout << "The destination string is: " << s2 << endl;  
    result1 = char_traits<char>::_Copy_s(s1,  
        char_traits<char>::length(s1), s2, 4);  
    cout << "The result1 = _Copy_s(s1, "  
         << "char_traits<char>::length(s1), s2, 4) is: "  
         << result1 << endl;  
}  

The source string is: abcd-1234-abcd  
The destination string is: ABCD-1234  
The result1 = _Copy_s(s1, char_traits<char>::length(s1), s2, 4) is: ABCD-1234-abcd  

Returns the end-of-file (EOF) character.

static int_type eof();

Return Value

The EOF character.

Remarks

A value that represents end of file (such as EOF or WEOF).

The C++ standard states that this value must not correspond to a valid char_type value. The Visual C++ compiler enforces this constraint for type char, but not for type wchar_t. The example below demonstrates this.

Example

// char_traits_eof.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
  
    char_traits<char>::char_type ch1 = 'x';  
    char_traits<char>::int_type int1;  
    int1 = char_traits<char>::to_int_type(ch1);  
    cout << "char_type ch1 is '" << ch1 << "' and corresponds to int_type "  
         << int1 << "." << endl << endl;  
  
    char_traits<char>::int_type int2 = char_traits<char>::eof();  
    cout << "The eof marker for char_traits<char> is: " << int2 << endl;  
  
    char_traits<wchar_t>::int_type int3 = char_traits<wchar_t>::eof();  
    cout << "The eof marker for char_traits<wchar_t> is: " << int3 << endl;  
}  

char_type ch1 is 'x' and corresponds to int_type 120.  
  
The eof marker for char_traits<char> is: -1  
The eof marker for char_traits<wchar_t> is: 65535  

Tests whether two char_type characters are equal.

static bool eq(const char_type& _Ch1, const char_type& _Ch2);

Parameters

_Ch1
The first of two characters to be tested for equality.

_Ch2
The second of two characters to be tested for equality.

Return Value

true if the first character is equal to the second character; otherwise false.

Example

// char_traits_eq.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
  
   char_traits<char>::char_type ch1 =  'x';  
   char_traits<char>::char_type ch2 =  'y';  
   char_traits<char>::char_type ch3 =  'x';  
  
   // Testing for equality  
   bool b1 = char_traits<char>::eq ( ch1 , ch2 );  
   if ( b1 )  
      cout << "The character ch1 is equal "  
           << "to the character ch2." << endl;  
   else  
      cout << "The character ch1 is not equal "  
           << "to the character ch2." << endl;  
  
   // An equivalent and alternatively test procedure  
   if ( ch1 == ch3 )  
      cout << "The character ch1 is equal "  
           << "to the character ch3." << endl;  
   else  
      cout << "The character ch1 is not equal "  
           << "to the character ch3." << endl;  
}  

The character ch1 is not equal to the character ch2.  
The character ch1 is equal to the character ch3.  

Tests whether two characters represented as int_types are equal or not.

static bool eq_int_type(const int_type& _Ch1, const int_type& _Ch2);

Parameters

_Ch1
The first of the two characters to be tested for equality as int_types.

_Ch2
The second of the two characters to be tested for equality as int_types.

Return Value

true if the first character is equal to the second character; otherwise false.

Example

// char_traits_eq_int_type.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   char_traits<char>::char_type ch1 =  'x';  
   char_traits<char>::char_type ch2 =  'y';  
   char_traits<char>::char_type ch3 =  'x';  
  
   // Converting from char_type to int_type  
   char_traits<char>::int_type int1, int2 , int3;  
   int1 =char_traits<char>:: to_int_type ( ch1 );  
   int2 =char_traits<char>:: to_int_type ( ch2 );  
   int3 =char_traits<char>:: to_int_type ( ch3 );  
  
   cout << "The char_types and corresponding int_types are:"  
        << "\n    ch1 = " << ch1 << " corresponding to int1 = "  
        << int1 << "."  
        << "\n    ch2 = " << ch2 << " corresponding to int1 = "  
        << int2 << "."  
        << "\n    ch3 = " << ch3 << " corresponding to int1 = "  
        << int3 << "." << endl << endl;  
  
   // Testing for equality of int_type representations  
   bool b1 = char_traits<char>::eq_int_type ( int1 , int2 );  
   if ( b1 )  
      cout << "The int_type representation of character ch1\n "  
           << "is equal to the int_type representation of ch2."  
           << endl;  
   else  
      cout << "The int_type representation of character ch1\n is "  
           << "not equal to the int_type representation of ch2."  
           << endl;  
  
   // An equivalent and alternatively test procedure  
   if ( int1 == int3 )  
      cout << "The int_type representation of character ch1\n "  
           << "is equal to the int_type representation of ch3."  
           << endl;  
   else  
      cout << "The int_type representation of character ch1\n is "  
           << "not equal to the int_type representation of ch3."  
           << endl;  
}  

The char_types and corresponding int_types are:  
    ch1 = x corresponding to int1 = 120.  
    ch2 = y corresponding to int1 = 121.  
    ch3 = x corresponding to int1 = 120.  
  
The int_type representation of character ch1  
 is not equal to the int_type representation of ch2.  
The int_type representation of character ch1  
 is equal to the int_type representation of ch3.  

Searches for the first occurrence of a specified character in a range of characters.

static const char_type* find(const char_type* str,
    size_t _Num,
    const char_type& _Ch);

Parameters

str
The first character in the string to be searched.

_Num
The number of positions, counting from the first, in the range to be searched.

_Ch
The character to be searched for in the range.

Return Value

A pointer to the first occurrence of the specified character in the range if a match is found; otherwise, a null pointer.

Example

// char_traits_find.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
  
   const char* s1 = "f2d-1234-abcd";  
   const char* result1;  
   cout << "The string to be searched is: " << s1 << endl;  
  
   // Searching for a 'd' in the first 6 positions of string s1  
   result1 = char_traits<char>::find ( s1 , 6 , 'd');  
   cout << "The character searched for in s1 is: "  
        << *result1 << endl;  
   cout << "The string beginning with the first occurrence\n "  
        << "of the character 'd' is: " << result1 << endl;  
  
   // When no match is found the NULL value is returned  
   const char* result2;  
   result2 = char_traits<char>::find ( s1 , 3 , 'a');  
   if ( result2 == NULL )  
      cout << "The result2 of the search is NULL." << endl;  
   else  
      cout << "The result2 of the search  is: " << result1  
           << endl;  
}  

The string to be searched is: f2d-1234-abcd  
The character searched for in s1 is: d  
The string beginning with the first occurrence  
 of the character 'd' is: d-1234-abcd  
The result2 of the search is NULL.  

An integer type that can represent a character of type char_type or an end-of-file (EOF) character.

typedef long int_type;  

Remarks

It must be possible to type cast a value of type CharType to int_type then back to CharType without altering the original value.

Example

See the example for eq_int_type for an example of how to declare and use int_type.

Returns the length of a string.

static size_t length(const char_type* str);

Parameters

str
The C-string whose length is to be measured.

Return Value

The number of elements in the sequence being measured, not including the null terminator.

Example

// char_traits_length.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   const char* str1= "Hello";  
   cout << "The C-string str1 is: " << str1 << endl;  
  
   size_t lenStr1;  
   lenStr1 = char_traits<char>::length ( str1 );  
   cout << "The length of C-string str1 is: "   
        << lenStr1 << "." << endl;  
}  

The C-string str1 is: Hello  
The length of C-string str1 is: 5.  

Tests whether one character is less than another.

static bool lt(const char_type& _Ch1, const char_type& _Ch2);

Parameters

_Ch1
The first of two characters to be tested for less than.

_Ch2
The second of two characters to be tested for less than.

Return Value

true if the first character is less than the second character; otherwise false.

Example

// char_traits_lt.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   char_traits<char>::char_type ch1 =  'x';  
   char_traits<char>::char_type ch2 =  'y';  
   char_traits<char>::char_type ch3 =  'z';  
  
   // Testing for less than  
   bool b1 = char_traits<char>::lt ( ch1 , ch2 );  
   if ( b1 )  
      cout << "The character ch1 is less than "  
           << "the character ch2." << endl;  
   else  
      cout << "The character ch1 is not less "  
           << "than the character ch2." << endl;  
  
   // An equivalent and alternatively test procedure  
   if ( ch3 <  ch2 )  
      cout << "The character ch3 is less than "  
           << "the character ch2." << endl;  
   else  
      cout << "The character ch3 is not less "  
           << "than the character ch2." << endl;  
}  

The character ch1 is less than the character ch2.  
The character ch3 is not less than the character ch2.  

Copies a specified number of characters in a sequence to another, possibly overlapping sequence.

This method is potentially unsafe, as it relies on the caller to check that the passed values are correct. Consider using char_traits::_Move_s instead.

static char_type *move(char_type* _To,
    const char_type* _From,
    size_t _Num);

Parameters

_To
The element at the beginning of the string or character array targeted to receive the copied sequence of characters.

_From
The element at the beginning of the source string or character array to be copied.

_Num
The number of elements to be copied from the source string.

Return Value

The first element _To copied into the string or character array targeted to receive the copied sequence of characters.

Remarks

The source and destination may overlap.

Example

// char_traits_move.cpp  
// compile with: /EHsc /W3  
#include <string>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
  
   char_traits<char>::char_type sFrom1[] =  "abcd-1234-abcd";  
   char_traits<char>::char_type sTo1[] =  "ABCD-1234";  
   char_traits<char>::char_type* result1;  
   cout << "The source string sFrom1 is: " << sFrom1 << endl;  
   cout << "The destination stringsTo1 is: " << sTo1 << endl;  
   // Note: char_traits::move is potentially unsafe, consider  
   // using char_traits::_Move_s instead.  
   result1 = char_traits<char>::move ( sTo1 ,  sFrom1 , 4 );  // C4996  
   cout << "The result1 = move ( sTo1 , sFrom1 , 4 ) is: "  
        << result1 << endl << endl;  
  
   // When source and destination overlap  
   char_traits<char>::char_type sToFrom2[] = "abcd-1234-ABCD";  
   char_traits<char>::char_type* result2;  
   cout << "The source/destination string sToFrom2 is: "  
        << sToFrom2 << endl;  
   const char* findc = char_traits<char>::find ( sToFrom2 , 4 , 'c' );  
   // Note: char_traits::move is potentially unsafe, consider  
   // using char_traits::_Move_s instead.  
   result2 = char_traits<char>::move ( sToFrom2 , findc , 8 );  // C4996  
   cout << "The result2 = move ( sToFrom2 , findc , 8 ) is: "  
        << result2 << endl;  
}  

The source string sFrom1 is: abcd-1234-abcd  
The destination stringsTo1 is: ABCD-1234  
The result1 = move ( sTo1 , sFrom1 , 4 ) is: abcd-1234  
  
The source/destination string sToFrom2 is: abcd-1234-ABCD  
The result2 = move ( sToFrom2 , findc , 8 ) is: cd-1234-4-ABCD  

Copies a specified number of characters in a sequence to another, possibly overlapping sequence.

static char_type *_Move_s(
    char_type* dest,  
    size_t dest_size,  
    const char_type* _From,  
    size_t count);

Parameters

dest
The element at the beginning of the string or character array targeted to receive the copied sequence of characters.

dest_size
The size of dest. If char_type is char, then this is in bytes. If char_type is wchar_t, then this is in words.

_From
The element at the beginning of the source string or character array to be copied.

count
The number of elements to be copied from the source string.

Return Value

The first element dest copied into the string or character array targeted to receive the copied sequence of characters.

Remarks

The source and destination may overlap.

Example

// char_traits__Move_s.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )  
{  
    using namespace std;  
  
    char_traits<char>::char_type sFrom1[] =  "abcd-1234-abcd";  
    char_traits<char>::char_type sTo1[] =  "ABCD-1234";  
    char_traits<char>::char_type* result1;  
    cout << "The source string sFrom1 is: " << sFrom1 << endl;  
    cout << "The destination stringsTo1 is: " << sTo1 << endl;  
    result1 = char_traits<char>::_Move_s(sTo1,  
        char_traits<char>::length(sTo1), sFrom1, 4);  
    cout << "The result1 = _Move_s(sTo1, "  
         << "char_traits<char>::length(sTo1), sFrom1, 4) is: "  
         << result1 << endl << endl;  
  
    // When source and destination overlap  
    char_traits<char>::char_type sToFrom2[] = "abcd-1234-ABCD";  
    char_traits<char>::char_type* result2;  
    cout << "The source/destination string sToFrom2 is: "  
         << sToFrom2 << endl;  
    const char* findc = char_traits<char>::find(sToFrom2, 4, 'c');  
    result2 = char_traits<char>::_Move_s(sToFrom2,  
        char_traits<char>::length(sToFrom2), findc, 8);  
    cout << "The result2 = _Move_s(sToFrom2, "  
        << "char_traits<char>::length(sToFrom2), findc, 8) is: "  
         << result2 << endl;  
}  

The source string sFrom1 is: abcd-1234-abcd  
The destination stringsTo1 is: ABCD-1234  
The result1 = _Move_s(sTo1, char_traits<char>::length(sTo1), sFrom1, 4) is: abcd-1234  
  
The source/destination string sToFrom2 is: abcd-1234-ABCD  
The result2 = _Move_s(sToFrom2, char_traits<char>::length(sToFrom2), findc, 8) is: cd-1234-4-ABCD  

Tests whether a character is not the end-of-file (EOF) character or is the EOF.

static int_type not_eof(const int_type& _Ch);

Parameters

_Ch
The character represented as an int_type to be tested for whether it is the EOF character or not.

Return Value

The int_type representation of the character tested, if the int_type of the character is not equal to that of the EOF character.

If the character int_type value is equal to the EOF int_type value, then false.

Example

// char_traits_not_eof.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( ) {  
   using namespace std;  
  
   char_traits<char>::char_type ch1 =  'x';  
   char_traits<char>::int_type int1;  
   int1 = char_traits<char>:: to_int_type ( ch1 );  
   cout << "The char_type ch1 is " << ch1  
        << " corresponding to int_type: "   
        << int1 << "." << endl;  
  
   // EOF member function  
   char_traits <char>::int_type int2 = char_traits<char>::eof ( );  
   cout << "The eofReturn is: " << int2 << endl;  
  
   // Testing for EOF or another character  
   char_traits <char>::int_type eofTest1, eofTest2;  
   eofTest1 = char_traits<char>::not_eof ( int1 );  
   if ( !eofTest1 )  
      cout << "The eofTest1 indicates ch1 is an EOF character."  
              << endl;  
   else  
      cout << "The eofTest1 returns: " << eofTest1   
           << ", which is the character: "   
           <<  char_traits<char>::to_char_type ( eofTest1 )  
           << "." << endl;  
  
   eofTest2 = char_traits<char>::not_eof ( int2 );  
   if ( !eofTest2 )  
      cout << "The eofTest2 indicates int2 is an EOF character."   
           << endl;  
   else  
      cout << "The eofTest1 returns: " << eofTest2   
           << ", which is the character: "   
           <<  char_traits<char>::to_char_type ( eofTest2 )   
           << "." << endl;  
}  

The char_type ch1 is x corresponding to int_type: 120.  
The eofReturn is: -1  
The eofTest1 returns: 120, which is the character: x.  
The eofTest2 indicates int2 is an EOF character.  

An integer type that can represent offsets between positions in a stream.

typedef streamoff off_type;  

Remarks

The type is a signed integer that describes an object that can store a byte offset involved in various stream positioning operations. It is typically a synonym for streamoff, but it has essentially the same properties as that type.

An integer type that can represent positions in a stream.

typedef streampos pos_type;  

Remarks

The type describes an object that can store all the information needed to restore an arbitrary file-position indicator within a stream. It is typically a synonym for streampos, but in any case it has essentially the same properties as that type.

A type that represents the conversion state for multibyte characters in a stream.

typedef implementation-defined state_type;  

Remarks

The type describes an object that can represent a conversion state. It is typically a synonym for mbstate_t, but in any case it has essentially the same properties as that type.

Converts an int_type character to the corresponding char_type character and returns the result.

static char_type to_char_type(const int_type& _Ch);

Parameters

_Ch
The int_type character to be represented as a char_type.

Return Value

The char_type character corresponding to the int_type character.

A value of _Ch that cannot be represented as such yields an unspecified result.

Remarks

The conversion operations to_int_type and to_char_type are inverse to each other, so that:

to_int_type ( to_char_type ( x ) ) == x

for any int_type x and

to_char_type ( to_int_type ( x ) ) == x

for any char_type x.

Example

// char_traits_to_char_type.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
  
   char_traits<char>::char_type ch1 =  'a';  
   char_traits<char>::char_type ch2 =  'b';  
   char_traits<char>::char_type ch3 =  'a';  
  
   // Converting from char_type to int_type  
   char_traits<char>::int_type int1, int2 , int3;  
   int1 =char_traits<char>:: to_int_type ( ch1 );  
   int2 =char_traits<char>:: to_int_type ( ch2 );  
   int3 =char_traits<char>:: to_int_type ( ch3 );  
  
   cout << "The char_types and corresponding int_types are:"  
        << "\n    ch1 = " << ch1 << " corresponding to int1 = "   
        << int1 << "."  
        << "\n    ch2 = " << ch2 << " corresponding to int1 = "   
        << int2 << "."  
        << "\n    ch3 = " << ch3 << " corresponding to int1 = "   
        << int3 << "." << endl << endl;  
  
   // Converting from int_type back to char_type  
   char_traits<char>::char_type rec_ch1;  
   rec_ch1 = char_traits<char>:: to_char_type ( int1);  
   char_traits<char>::char_type rec_ch2;  
   rec_ch2 = char_traits<char>:: to_char_type ( int2);  
  
   cout << "The recovered char_types and corresponding int_types are:"  
        << "\n    recovered ch1 = " << rec_ch1 << " from int1 = "   
        << int1 << "."  
        << "\n    recovered ch2 = " << rec_ch2 << " from int2 = "   
        << int2 << "." << endl << endl;  
  
   // Testing that the conversions are inverse operations  
   bool b1 = char_traits<char>::eq ( rec_ch1 , ch1 );  
   if ( b1 )  
      cout << "The recovered char_type of ch1"  
           << " is equal to the original ch1." << endl;  
   else  
      cout << "The recovered char_type of ch1"  
           << " is not equal to the original ch1." << endl;  
  
   // An equivalent and alternatively test procedure  
   if ( rec_ch2 == ch2 )  
      cout << "The recovered char_type of ch2"  
           << " is equal to the original ch2." << endl;  
   else  
      cout << "The recovered char_type of ch2"  
           << " is not equal to the original ch2." << endl;  
}  

The char_types and corresponding int_types are:  
    ch1 = a corresponding to int1 = 97.  
    ch2 = b corresponding to int1 = 98.  
    ch3 = a corresponding to int1 = 97.  
  
The recovered char_types and corresponding int_types are:  
    recovered ch1 = a from int1 = 97.  
    recovered ch2 = b from int2 = 98.  
  
The recovered char_type of ch1 is equal to the original ch1.  
The recovered char_type of ch2 is equal to the original ch2.  

Converts a char_type character to the corresponding int_type character and returns the result.

static int_type to_int_type(const char_type& _Ch);

Parameters

_Ch
The char_type character to be represented as an int_type.

Return Value

The int_type character corresponding to the char_type character.

Remarks

The conversion operations to_int_type and to_char_type are inverse to each other, so that:

to_int_type ( to_char_type ( x ) ) == x

for any int_type x, and

to_char_type ( to_int_type ( x ) ) == x

for any char_type x.

Example

// char_traits_to_int_type.cpp  
// compile with: /EHsc  
#include <string>  
#include <iostream>  
  
int main( )   
{  
   using namespace std;  
   char_traits<char>::char_type ch1 = 'a';  
   char_traits<char>::char_type ch2 = 'b';  
   char_traits<char>::char_type ch3 = 'a';  
  
   // Converting from char_type to int_type  
   char_traits<char>::int_type int1, int2 , int3;  
   int1 =char_traits<char>:: to_int_type ( ch1 );  
   int2 =char_traits<char>:: to_int_type ( ch2 );  
   int3 =char_traits<char>:: to_int_type ( ch3 );  
  
   cout << "The char_types and corresponding int_types are:"  
        << "\n    ch1 = " << ch1 << " corresponding to int1 = "   
        << int1 << "."  
        << "\n    ch2 = " << ch2 << " corresponding to int1 = "   
        << int2 << "."  
        << "\n    ch3 = " << ch3 << " corresponding to int1 = "   
        << int3 << "." << endl << endl;  
  
   // Converting from int_type back to char_type  
   char_traits<char>::char_type rec_ch1;  
   rec_ch1 = char_traits<char>:: to_char_type ( int1);  
   char_traits<char>::char_type rec_ch2;  
   rec_ch2 = char_traits<char>:: to_char_type ( int2);  
  
   cout << "The recovered char_types and corresponding int_types are:"  
        << "\n    recovered ch1 = " << rec_ch1 << " from int1 = "   
        << int1 << "."  
        << "\n    recovered ch2 = " << rec_ch2 << " from int2 = "   
        << int2 << "." << endl << endl;  
  
   // Testing that the conversions are inverse operations  
   bool b1 = char_traits<char>::eq ( rec_ch1 , ch1 );  
   if ( b1 )  
      cout << "The recovered char_type of ch1"  
           << " is equal to the original ch1." << endl;  
   else  
      cout << "The recovered char_type of ch1"  
           << " is not equal to the original ch1." << endl;  
  
   // An equivalent and alternatively test procedure  
   if ( rec_ch2 == ch2 )  
      cout << "The recovered char_type of ch2"  
           << " is equal to the original ch2." << endl;  
   else  
      cout << "The recovered char_type of ch2"  
           << " is not equal to the original ch2." << endl;  
}  

The char_types and corresponding int_types are:  
    ch1 = a corresponding to int1 = 97.  
    ch2 = b corresponding to int1 = 98.  
    ch3 = a corresponding to int1 = 97.  
  
The recovered char_types and corresponding int_types are:  
    recovered ch1 = a from int1 = 97.  
    recovered ch2 = b from int2 = 98.  
  
The recovered char_type of ch1 is equal to the original ch1.  
The recovered char_type of ch2 is equal to the original ch2.  

Thread Safety in the C++ Standard Library

Show: