Freigeben über


char_traits::to_char_type

Konvertiert ein int_type Zeichen in den entsprechenden char_type Zeichen und gibt das Ergebnis zurück.

static char_type to_char_type(
   const int_type& _Ch
);

Parameter

  • _Ch
    Das als char_type dargestellt zu werden int_type Zeichen.

Rückgabewert

Das char_type Zeichen entsprechend dem int_type Zeichen.

Ein Wert von _Ch, der nicht als solche zurückgehalten dargestellt werden kann ein nicht definiertes Ergebnis.

Hinweise

Die Konvertierungsoperationen to_int_type und to_char_type sind zueinander umgekehrt, damit:

== ( xto_int_typeto_char_type (x))

für ein beliebiges x und int_type

== ( xto_char_typeto_int_type (x))

char_type für ein beliebiges x.

Beispiel

// 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;
}
  
  
  
  
  
  
  

Anforderungen

Header: <string>

Namespace: std

Siehe auch

Referenz

char_traits Struct