The C++ < and > operators for string comparison 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 C++ < and > operators for string to test if the string object on the left side of the operator is less than to the string object on the right side and the string object on the left side of the operator is greater than to the string object on the right side respectively

To show: How to use the < and > C++ operators to test if the string object on the left side of the operator is less than to the string object on the right side and the string object on the left side of the operator is greater than to the string object on the right side respectively

 

// the C++ < and > operators for string comparison

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// declaring objects of type basic_string<char>

string str1("testingthree");

string str2("testingtwo");

cout<<"str1 is = "<<str1<<endl;

cout<<"str2 is = "<<str2<<endl;

 

// declaring a C-style string

char *str3 = "testingone";

cout<<"str3 C-style string is = "<<str3<<endl;

 

// comparison between left-side object of type basic_string and right-side object of type basic_string

cout<<"\nOperation: (str1 < str2)"<<endl;

if(str1 < str2)

cout<<"str1 is less then string str2."<<endl;

else

cout<<"str1 is not less then string str2."<<endl;

 

// comparison between left-side object of C-style string type and right-side object of type basic_string

cout<<"\nOperation: (str3 < str2)"<<endl;

if(str3 < str2)

cout<<"str3 is less then string str2."<<endl;

else

cout<<"str3 is not less then string str2."<<endl;

 

// comparison between left-side object of type basic_string and right-side object of C-style string type

cout<<"\nOperation: (str1 < str3)"<<endl;

if(str1 < str3)

cout<<"str1 is less then string str3."<<endl;

else

cout<<"str1 is not less then string str3."<<endl;

 

// comparison between left-side object of type basic_string and right-side object of type basic_string

cout<<"\nOperation: (str1 > str2)"<<endl;

if(str1 > str2)

cout<<"str1 is greater then string str2."<<endl;

else

cout<<"str1 is not greater then string str2."<<endl;

 

// comparison between left-hand object of C-style string type and right-hand object of type basic_string

cout<<"\nOperation: (str3 > str2)"<<endl;

if(str3 > str2)

cout<<"str3 is greater then string str2."<<endl;

else

cout<<"str3 is not greater then string str2."<<endl;

 

// comparison between left-hand object of type basic_string and right-hand object of C-style string type

cout<<"\nOperation: (str1 > str3)"<<endl;

if(str1 > str3)

cout<<"str1 is greater then string str3."<<endl;

else

cout<<"str1 is not greater then string str3."<<endl;

return 0;

}

 

Output example:

 

str1 is = testingthree

str2 is = testingtwo

str3 C-style string is = testingone

 

Operation: (str1 < str2)

str1 is less then string str2.

 

Operation: (str3 < str2)

str3 is less then string str2.

 

Operation: (str1 < str3)

str1 is not less then string str3.

 

Operation: (str1 > str2)

str1 is not greater then string str2.

 

Operation: (str3 > str2)

str3 is not greater then string str2.

 

Operation: (str1 > str3)

str1 is greater then string str3.

Press any key to continue . . .

 

 

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