当前位置:  开发笔记 > 编程语言 > 正文

C++字符串.为什么答案显示字符串"dog"大于"cat",那么"cat"大于"dog"?

如何解决《C++字符串.为什么答案显示字符串"dog"大于"cat",那么"cat"大于"dog"?》经验,为你挑选了1个好方法。

我不太清楚为什么较大的字符串("cat"和"dog")的答案不一致.我正在使用链接列表和模板的使用做一些事情.我的好奇心让我修改了模板和函数重载.如果有人能解释发生了什么,我将不胜感激.谢谢.

#include 
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 
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 
elementType anyLarger(elementType parameter1, elementType parameter2)
{
    if (parameter1 >= parameter2)
      return parameter1; 
    else
      return parameter2;
}

这是运行程序后的结果.

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

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

更新:进行了一些更改并使用了strcmp
#include 
#include 
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 
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 
elementType anyLarger(elementType parameter1, elementType parameter2)
{
  if (parameter1 >= parameter2)
    return parameter1; 
  else
    return parameter2;
}

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

更新:输出
// 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"

Jon.. 10

在两种情况下,您都不会按字典顺序比较字符串.

"cat" >= "dog"

这是比较char指针,因为文字的类型"cat""dog"const char*.它可以给出任何结果,具体取决于编译器如何决定实现文字.

要按字典顺序比较C风格的字符串(包括这些字符串),strcmp必须使用该函数.

strCat >= strDog

这是按字典顺序比较字符串,因为std::string它提供了明确实现此类比较的比较运算符.



1> Jon..:

在两种情况下,您都不会按字典顺序比较字符串.

"cat" >= "dog"

这是比较char指针,因为文字的类型"cat""dog"const char*.它可以给出任何结果,具体取决于编译器如何决定实现文字.

要按字典顺序比较C风格的字符串(包括这些字符串),strcmp必须使用该函数.

strCat >= strDog

这是按字典顺序比较字符串,因为std::string它提供了明确实现此类比较的比较运算符.


哎呀,它可以给出[两个结果](https://markshroyer.com/2012/06/c-both-true-and-false/)因为它是未定义的行为比较(排除(in)相等)两个"无关"指针.
推荐阅读
有风吹过best
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有