C++字符串。为什么答案显示字符串"狗"大于"猫",然后"猫"大于"狗"?
C++ string. why is the answer showing that string "dog" is greater than "cat", then "cat" is greater than "dog"?
我不太清楚为什么较大字符串("cat"answers"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
提供了显式实现这种比较的比较运算符。
"cat" >= "dog"
您比较的是指针,而不是实际值。使用strcmp
。
这是因为字符串文字(例如"cat"
)的类型不是std::string
。您正在对两个不相关的const char
数组进行指针比较。
当您对文字字符串"cat"
和"dog"
进行比较时,它们实际上是指向字符数组的指针,因此最大的指针将恰好存储在内存中较高位置的指针。
string larger(string y, string z)
之所以有效,是因为std::string
可以由char*
构造,因此"cat"
和"dog"
在进行比较之前会转换为std::string
对象。
另一方面,模板函数接受它们作为它们的实际类型,当作为参数传递时,这些类型将变为const char*
,并且只比较它们的地址。
正如其他人已经提到的,在您的行中
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;
}