C ++ string. why does the answer show that the string “dog” is larger than “cat” and then “cat” is larger than “dog”?

I'm not quite sure why the answer to the large string ("cat" and "dog") is incompatible. I did some things with linked lists and using templates. My curiosity prompted me to revise the templates and overload the functions. If anyone can explain what is happening, I would appreciate it. Thanks.

#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 starting 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: some changes are made and strcmp is used

#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: Exit

// 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"
+4
source share
5 answers

"cat" "dog" , , , .

string larger(string y, string z) , std::string char*, "cat" "dog" std::string .

, , , const char* .

+3

.

"cat" >= "dog"

char, "cat" "dog" const char*. , , .

C- ( , ) strcmp.

strCat >= strDog

, std::string , .

+10
"cat" >= "dog"

, . strcmp.

+4

, (, "cat") std::string. const char.

+4

,

cout << "anyLarger(cat, dog)       =  " << anyLarger("cat", "dog") << endl;

anyLarger<const char &[]>, .. anyLarger , . , std::string :

larger("cat", "dog");
anyLarger<std::string>("cat", "dog");
anyLarger(std::string("cat"), std::string("dog"));
anyLarger("cat"s, "dog"s); // C++14

( C, , ):

template <>
elementType anyLarger<char*>(char* parameter1, char* parameter2)
{
    if (strcmp(parameter1, parameter2) >= 0)
      return parameter1; 
    else
      return parameter2;
}
+3

Source: https://habr.com/ru/post/1619566/


All Articles