Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ string. why is the answer showing that string "dog" is greater than "cat", then "cat" is greater than "dog"?

Tags:

c++

I'm not quite sure why the answer for the larger string ("cat" and "dog") is not consistent. I was doing some things with Linked Lists and the use of templates. My curiosity got me to revise templates and function overloading. If anyone can explain what is going on, I would appreciate it. Thank you.

#include <iostream>
using namespace std;   // for the sake of simplicity. (otherwise, std::)

// Function overloading and the use of templates

// overloading the function larger
int larger(int, int);
char larger(char, char);
double larger(double, double);
string larger(string, string);

template <class elementType>
elementType anyLarger(elementType parameter1, elementType parameter2);


int main() {

    cout << endl;
    cout << "Function Overloading" << endl;

    cout << "larger(15, 27)            =  " << larger(15, 27) << endl;
    cout << "larger('X', 'P')          =  " << larger('X', 'P') << endl;
    cout << "larger(4.9, 3.2)          =  " << larger(4.9, 3.2) << endl;
    cout << "larger(cat, dog)          =  " << larger("cat", "dog") << endl;

    cout << endl;
    cout << "Using the function template to find the larger of two items" << endl;
    cout << "anyLarger(15, 27)         =  " << anyLarger(15, 27) << endl;
    cout << "anyLarger('X', 'P')       =  " << anyLarger('X', 'P') << endl;
    cout << "anyLarger(4.9, 3.2)       =  " << anyLarger(4.9, 3.2) << endl;
    cout << "anyLarger(cat, dog)       =  " << anyLarger("cat", "dog") << endl;
    cout << endl;


    cout << "Compare two strings:   cat, dog" << endl;
    if ("cat" >= "dog") {
      cout << "cat is greater than dog" << endl;
    }
    else {
      cout << "dog is greater than cat" << endl;
    }

    cout << endl;
    string strCat = "cat";
    string strDog = "dog";
    cout << "string strCat = cat" << endl;
    cout << "string strDog = dog" << endl;
    if (strCat >= strDog) {
      cout << "strCat is greater than strDog" << endl;
    }
    else {
      cout << "strDog is greater than strCat" << endl;
    }
    cout << endl;
} // end main 

// Overloading larger
int larger(int x, int y) {
    if (x >= y)
      return x;
    else 
      return y;
}

char larger(char a, char b) {
    if (a >= b)
      return a;
    else 
      return b;
}

double larger(double p, double q) {
    if (p >= q)
      return p;
    else
      return q;
}

string larger(string y, string z) {
    if (y >= z)
      return y;
    else
      return z;
}


// Defining the template function
template <class elementType>
elementType anyLarger(elementType parameter1, elementType parameter2)
{
    if (parameter1 >= parameter2)
      return parameter1; 
    else
      return parameter2;
}

Here is the result after running the program.

Function Overloading
larger(15, 27)            =  27
larger('X', 'P')          =  X
larger(4.9, 3.2)          =  4.9
larger(cat, dog)          =  dog

Using the function template to find the larger of two items
anyLarger(15, 27)         =  27
anyLarger('X', 'P')       =  X
anyLarger(4.9, 3.2)       =  4.9
anyLarger(cat, dog)       =  cat

Compare two strings:   cat, dog
cat is greater than dog

string strCat = cat
string strDog = dog
strDog is greater than strCat

==============================================

Update: Made some changes and used strcmp

#include <iostream>
#include <string.h>
using namespace std;

// Function overloading and the use of templates


// overloading the function larger
int larger(int, int);
char larger(char, char);
double larger(double, double);
string larger(string, string);


template <class elementType>
elementType anyLarger(elementType parameter1, elementType parameter2);


int main(){

  cout << endl;
  cout << "// Function Overloading" << endl;


  cout << "larger(15, 27)              =  " << larger(15, 27) << endl;
  cout << "larger('X', 'P')            =  " << larger('X', 'P') << endl;
  cout << "larger(4.9, 3.2)            =  " << larger(4.9, 3.2) << endl;
  cout << "larger(\"cat\", \"dog\")        =  " << larger("cat", "dog") << endl;

  cout << endl;
  cout << "// Using the function template to find the larger of two items" << endl;
  cout << "anyLarger(15, 27)           =  " << anyLarger(15, 27) << endl;
  cout << "anyLarger('X', 'P')         =  " << anyLarger('X', 'P') << endl;
  cout << "anyLarger(4.9, 3.2)         =  " << anyLarger(4.9, 3.2) << endl;
  cout << "anyLarger(\"cat\", \"dog\")     =  " << anyLarger("cat", "dog") << endl;
  cout << endl;


  cout << "// Compare two strings using >= :   \"cat\", \"dog\"" << endl;
  if ("cat" >= "dog") {
    cout << "\"cat\" is greater than \"dog\"" << endl;
  }
  else {
    cout << "\"dog\" is greater than \"cat\"" << endl;
  }

  cout << endl;
  cout << "// The use of variables: strCat and strDog. Compare using >=" << endl;
  string strCat = "cat";
  string strDog = "dog";
  cout << "string strCat = \"cat\";" << endl;
  cout << "string strDog = \"dog\";" << endl;
  if (strCat >= strDog) {
    cout << "strCat is greater than strDog" << endl;
  }
  else {
    cout << "strDog is greater than strCat" << endl;
  }
  cout << endl;


  cout << "// Using strcmp.   strcmp(\"cat\", \"dog\")" << endl; 
  int result = strcmp("cat", "dog");
  if (result > 0) {
    cout << "\"cat\" is greater than \"dog\"" << endl;
  }
  else {
    cout << "\"dog\" is greater than \"cat\"" << endl;
  }
}

// Overloading larger
int larger(int x, int y) {
if (x >= y)
    return x;
  else 
    return y;
}


char larger(char a, char b) {
  if (a >= b)
    return a;
  else 
    return b;
}


double larger(double p, double q) {
  if (p >= q)
    return p;
  else
    return q;
}

string larger(string y, string z) {
  if (y >= z)
    return y;
  else
    return z;
}


// Defining the template function
template <class elementType>
elementType anyLarger(elementType parameter1, elementType parameter2)
{
  if (parameter1 >= parameter2)
    return parameter1; 
  else
    return parameter2;
}

================

Update: Output

// Function Overloading
larger(15, 27)              =  27
larger('X', 'P')            =  X
larger(4.9, 3.2)            =  4.9
larger("cat", "dog")        =  dog

// Using the function template to find the larger of two items
anyLarger(15, 27)           =  27
anyLarger('X', 'P')         =  X
anyLarger(4.9, 3.2)         =  4.9
anyLarger("cat", "dog")     =  cat

// Compare two strings using >= :   "cat", "dog"
"cat" is greater than "dog"

// The use of variables: strCat and strDog. Compare using >=
string strCat = "cat";
string strDog = "dog";
strDog is greater than strCat

// Using strcmp.   strcmp("cat", "dog")
"dog" is greater than "cat"
like image 552
User_fts Avatar asked Nov 28 '22 06:11

User_fts


2 Answers

"cat" >= "dog"

You are comparing pointers but not the actual values. Use strcmp.

like image 37
Mahesh Avatar answered Nov 30 '22 19:11

Mahesh


You are not lexicographically comparing strings in both cases.

"cat" >= "dog"

This is comparing char pointers because the type of the literals "cat" and "dog" is const char*. It could give either result, depending on how the compiler decided to materialize the literals.

To lexicographically compare C-style strings (including literals like these) the function strcmp must be used instead.

strCat >= strDog

This is comparing strings lexicographically because std::string provides comparison operators that explicitly implement this kind of comparison.

like image 89
Jon Avatar answered Nov 30 '22 21:11

Jon