The C++ char_traits, eq_int_type() and to_int_type() class member functions program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using eq_int_type to test whether two characters represented as int_types are equal or not and to_int_type to converts a char_type character to the corresponding int_type character and returns the result in C++ programming
To show: How to use the C++ eq_int_type() and to_int_type() char_traits class members in C++ programming
// the C++ char_traits members, eq_int_type() and to_int_type() example
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
char_traits<char>::char_type chr1 = 'P';
char_traits<char>::char_type chr2 = 'Q';
char_traits<char>::char_type chr3 = 'P';
char_traits<char>::char_type chr4 = 'r';
// char_type to int_type conversion
char_traits<char>::int_type int1, int2, int3, int4;
int1 = char_traits<char>::to_int_type(chr1);
int2 = char_traits<char>::to_int_type(chr2);
int3 = char_traits<char>::to_int_type(chr3);
int4 = char_traits<char>::to_int_type(chr4);
cout<<"Operation: to_int_type(character)"<<endl;
cout<<"The char_types and corresponding int_types are:\n";
cout<<chr1<<" = "<<int1<<endl;
cout<<chr2<<" = "<<int2<<endl;
cout<<chr4<<" = "<<int4<<endl;
// equality of int_type representations test
cout<<"\nOperation: eq_int_type(int1, int2)"<<endl;
bool var1 = char_traits<char>::eq_int_type(int1, int2);
if(var1)
cout<<"The int_type representation of characters chr1\n"
<<"and chr2 is equal."<<endl;
else
cout<<"The int_type representation of characters chr1\n"
<<"and chr2 is not equal."<<endl;
// alternatively
cout<<"\nOperation: int1 == int3"<<endl;
if(int1 == int3)
cout<<"The int_type representation of characters chr1\n"
<<"and chr3 is equal."<<endl;
else
cout<<"The int_type representation of characters chr1\n"
<<"and chr3 is not equal."<<endl;
return 0;
}
Output example:
Operation: to_int_type(character)
The char_types and corresponding int_types are:
P = 80
Q = 81
r = 114
Operation: eq_int_type(int1, int2)
The int_type representation of characters chr1
and chr2 is not equal.
Operation: int1 == int3
The int_type representation of characters chr1
and chr3 is equal.
Press any key to continue . . .