我不太确定为什么大字符串(" cat"和#34; dog")的答案不一致。我正在使用链接列表和模板的使用做一些事情。我的好奇心让我修改了模板和函数重载。如果有人能解释发生了什么,我将不胜感激。 谢谢。
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
这是运行程序后的结果。
#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;
}
============================================== < / p>
// 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"
=====
border-radius
答案 0 :(得分:10)
在两种情况下,你都没有按字典顺序比较字符串。
"cat" >= "dog"
这是比较char
指针,因为文字"cat"
和"dog"
的类型是const char*
。它可以给出任何结果,具体取决于编译器决定如何实现文字。
要按字典顺序比较C风格的字符串(包括这些字符串),必须使用函数strcmp
。
strCat >= strDog
这是按字典顺序比较字符串,因为std::string
提供了明确实现此类比较的比较运算符。
答案 1 :(得分:4)
"cat" >= "dog"
您正在比较指针而不是实际值。使用strcmp
。
答案 2 :(得分:4)
这是因为字符串文字(例如"cat"
)不属于std::string
类型。您正在对两个不相关的const char
数组进行指针比较。
答案 3 :(得分:3)
当您比较它们时,文字字符串"cat"
和"dog"
实际上是指针到字符数组,所以最大的将是恰好存储的字符串在更高的位置记忆。
您的string larger(string y, string z)
有效,因为std::string
可以由char*
构建,因此"cat"
和"dog"
会被转换为std::string
个对象。比较。
另一方面,模板函数将它们作为实际类型接受,当它们作为参数传递时,变为const char*
并且仅对它们的地址进行比较。
答案 4 :(得分:3)
正如其他人已经提到的那样,在你的行中
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;
}