The C++ >= and <= operators for string 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: Tests if the string object on the left side of the operator is less than or equal to the string object on the right side using C++ <= operator and tests if the string object on the left side of the operator is greater than or equal to the string object on the right side using C++ >= operator

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

 

// the C++ >= and <= operators for string program example

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// declaring an objects of type basic_string<char>

string str1("testingone");

string str2("testingtwo");

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

cout<<"str2 string 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 than or equal to str2."<<endl;

else

cout<<"str1 is not less than or equal to 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 than or equal to str2."<<endl;

else

cout<<"str3 is not less than or equal to 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 than or equal to str3."<<endl;

else

cout<<"str1 is not less than or equal to 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 than or equal to str2."<<endl;

else

cout<<"str1 is not greater than or equal to 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 than or equal to str2."<<endl;

else

cout<<"str3 is not greater than or equal to 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 than or equal to str3."<<endl;

else

cout<<"str1 is not greater than or equal to str3."<<endl;

 

return 0;

}

 

Output example:

 

str1 string is = testingone

str2 string is = testingtwo

str3 C-style string is = testingone

 

Operation: (str1 <= str2)

str1 is less than or equal to str2.

 

Operation: (str3 <= str2)

str3 is less than or equal to str2.

 

Operation: (str1 <= str3)

str1 is less than or equal to str3.

 

Operation: (str1 >= str2)

str1 is not greater than or equal to str2.

 

Operation: (str3 >= str2)

str3 is not greater than or equal to str2.

 

Operation: (str1 >= str3)

str1 is greater than or equal to str3.

Press any key to continue . . .

 

 

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