The C++ compare() member function code sample part II
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++ compare() member function to compare a string with a specified string to determine if the two strings are equal or if one is lexicographically less than the other
To show: How to use the C++ compare() member function in C++ programming to compare a string with a specified string to determine if the two strings are equal or if one is lexicographically less than the other part II
// using the C++ compare() example part II
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
int str8, str11, str14, str17;
string str9("TestFourth");
string str10("TFourthT");
cout<<"str9 string is: "<<str9<<endl;
cout<<"str10 string is: "<<str10<<endl;
str8 = str9.compare(4, 6, str10, 1, 6);
// comparing part of a string to part of a string
cout<<"Operation: str9.compare(4, 6, str10, 1, 6)"<<endl;
if(str8 < 0)
cout<<"The 6 characters from position 4 of the str9 string are less than "
<<"the 6 characters str10 string from position 1."<<endl;
else if(str8 == 0)
cout<<"The 6 characters from position 4 of the str9 string are equal to "
<<"the 6 characters str10 string from position 1."<<endl;
else
cout<<"The 6 characters from position 4 of the str9 string is greater than "
<<"the 6 characters str10 string from position 1."<<endl;
cout<<endl;
// comparing a string to a C-string
string str12("Fifth");
const char* str13 ="Sixth";
cout<<"The str12 string is: "<<str12<<endl;
cout<<"The str13 C-string is: "<<str13<<endl;
str11 = str12.compare(str13);
cout<<"Operation: str12.compare(str13)"<<endl;
if(str11 < 0)
cout<<"The str12 string is less than the str13 C-string."<<endl;
else if(str11 == 0)
cout<<"The str12 string is equal to the str13 C-string."<<endl;
else
cout<<"The str12 string is greater than the str13 C-string."<<endl;
cout << endl;
// comparing part of a string to a C-string
string str15("SeventhEight");
const char* str16 ="Eight";
cout<<"str15 string is: "<<str15<<endl;
cout<<"str16 string is: "<<str16<<endl;
str14 = str15.compare(7, 5, str16);
cout<<"Operation: str15.compare(7, 5, str16)"<<endl;
if(str14 < 0)
cout<<"The last 5 characters of the str15 string are less than "
<<"the str16 C-string."<<endl;
else if(str14 == 0)
cout<<"The last 5 characters of the str15 string are equal to "
<<"the str16 C-string."<<endl;
else
cout<<"The last 5 characters of the str15 string is greater than "
<<"the str16 C-string."<<endl;
cout << endl;
// comparing part of a string to part of an equal length of a C-string
string str18("ReTestEighth");
const char* str19 ="TestEighth";
cout<<"str18 string is: "<<str18<<endl;
cout<<"str19 C-string is: "<<str19<<endl;
str17 = str18.compare(2, 4, str19, 4);
cout<<"Operation: str18.compare(4, 6, str19, 6)"<<endl;
if(str17 < 0)
cout<<"The 4 characters from position 2 of the str18 string are less than "
<<"the first 4 characters of the str19 C-string."<<endl;
else if(str17 == 0)
cout<<"The 4 characters from position 2 of the str18 string are equal to "
<<"the first 4 characters of the str19 C-string."<<endl;
else
cout<<"The 4 characters from position 2of the str18 string is greater than "
<<"the first 4 characters of the str19 C-string."<<endl;
return 0;
}
Output example:
str9 string is: TestFourth
str10 string is: TFourthT
Operation: str9.compare(4, 6, str10, 1, 6)
The 6 characters from position 4 of the str9 string are equal to the 6 characters str10 string from position 1.
The str12 string is: Fifth
The str13 C-string is: Sixth
Operation: str12.compare(str13)
The str12 string is less than the str13 C-string.
str15 string is: SeventhEight
str16 string is: Eight
Operation: str15.compare(7, 5, str16)
The last 5 characters of the str15 string are equal to the str16 C-string.
str18 string is: ReTestEighth
str19 C-string is: TestEighth
Operation: str18.compare(4, 6, str19, 6)
The 4 characters from position 2 of the str18 string are equal to the first 4 characters of the str19 C-string.
Press any key to continue . . .