The C++ char_traits class member function: lt(), to test whether one character is less than another C++ 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 the lt() member function to test whether one character is less than another in C++ programming

To show: How to use the lt() member function to test whether one character is less than another in C++ programming

 

// the char_traits class member, lt(), to test whether one character is less than another

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

char_traits<char>::char_type chr1 = '1';

char_traits<char>::char_type chr2 = 'q';

char_traits<char>::char_type chr3 = 'R';

char_traits<char>::int_type int1, int2, int3;

 

int1 = char_traits<char>::to_int_type(chr1);

int2 = char_traits<char>::to_int_type(chr2);

int3 = char_traits<char>::to_int_type(chr3);

 

// char_type to int_type conversion, for testing

cout<<"chr1 = "<<chr1<<", chr2 = "<<chr2<<", chr3 = "<<chr3<<endl;

cout<<"chr1 = "<<int1<<", chr2 = "<<int2<<", chr3 = "<<int3<<endl;

 

// testing for less than

cout<<"\nOperation: lt(chr1, chr2)"<<endl;

bool var1 = char_traits<char>::lt(chr1, chr2);

if(var1)

cout<<"The chr1 is less than "

<<"the chr2."<<endl;

else

cout<<"The chr1 is not less "

<<"than the chr2."<<endl;

 

// alternatively

cout<<"\nOperation: chr2 < chr3"<<endl;

if(chr2 < chr3)

cout<<"The chr2 is less than "

<<"the chr3."<<endl;

else

cout<<"The chr2 is not less "

<<"than the chr3."<<endl;

 

return 0;

}

 

Output example:

 

chr1 = 1, chr2 = q, chr3 = R

chr1 = 49, chr2 = 113, chr3 = 82

 

Operation: lt(chr1, chr2)

The chr1 is less than the chr2.

 

Operation: chr2 < chr3

The chr2 is not less than the chr3.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index