gpt4 book ai didi

C++ 字符串。为什么答案显示字符串 "dog"大于 "cat",然后 "cat"大于 "dog"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:54:30 31 4
gpt4 key购买 nike

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

#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;
}

这是运行程序后的结果。

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 <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;
}

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

更新:输出

// 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"

最佳答案

在这两种情况下,您都没有按字典顺序比较字符串。

"cat" >= "dog"

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

要按字典顺序比较 C 风格的字符串(包括像这样的文字),必须使用 strcmp 函数。

strCat >= strDog

这是按字典顺序比较字符串,因为 std::string 提供了显式实现这种比较的比较运算符。

关于C++ 字符串。为什么答案显示字符串 "dog"大于 "cat",然后 "cat"大于 "dog"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34232689/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com