Check out example codes for "what does compare function do in c++". It will help you in understanding the concepts better.

Code Example 1

// CPP code for demonstrating  
// string::compare (const string& str) const 
  
#include<iostream> 
using namespace std; 
  
void compareOperation(string s1, string s2) 
{ 
    // returns a value < 0 (s1 is smaller then s2) 
    if((s1.compare(s2)) < 0) 
        cout << s1 << " is smaller than " << s2 << endl; 
  
    // returns 0(s1, is being comapared to itself) 
    if((s1.compare(s1)) == 0) 
        cout << s1 << " is equal to " << s1 << endl; 
    else
        cout << "Strings didn't match "; 
      
} 
  
// Driver Code 
int main() 
{ 
    string s1("Geeks"); 
    string s2("forGeeks"); 
    compareOperation(s1, s2); 
      
    return 0;  
}

Code Example 2

int compare (const string& str) const;

Code Example 3

// comparing apples with apples
#include <iostream>
#include <string>

int main ()
{
  std::string str1 ("green apple");
  std::string str2 ("red apple");

  if (str1.compare(str2) != 0)
    std::cout << str1 << " is not " << str2 << '\n';

  if (str1.compare(6,5,"apple") == 0)
    std::cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    std::cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    std::cout << "therefore, both are apples\n";

  return 0;
}

Code Example 4

// CPP code to implement relational 
// operators on string objects 
#include <iostream> 
using namespace std; 
  
void relationalOperation(string s1, string s2) 
{ 
  
    if (s1 != s2) 
        cout << s1 << " is not equal to "
             << s2 << endl; 
    if (s1 > s2) 
        cout << s1 << " is greater than "
             << s2 << endl; 
    else
        cout << s2 << " is greater than "
             << s1 << endl; 
} 
  
// Main function 
int main() 
{ 
    string s1("Geeks"); 
    string s2("forGeeks"); 
    relationalOperation(s1, s2); 
    return 0; 
}

Learn ReactJs, React Native from akashmittal.com