当我阅读“ C ++ Primer”(第4版)第16.1章时,有一个简单的模板演示:

// implement strcmp-like generic compare function
// returns 0 if the values are equal, 1 if v1 is larger, -1 if v1 is smaller
template <typename T>
int compare(const T &v1, const T &v2)
{
    if (v1 < v2) return -1;
    if (v2 < v1) return 1;
        return 0;
}


主要功能的调用:

int main ()
{
    // T is int;
    // compiler instantiates int compare(const int&, const int&)
    cout << compare(1, 0) << endl;
    // T is string;
    // compiler instantiates int compare(const string&, const string&)
    string s1 = "hi", s2 = "world";
    cout << compare(s1, s2) << endl;
    return 0;
}


我对这段代码进行了一些修改:

//----------------------------test.cpp start----------------
#include <iostream>
#include <string>

using namespace std;

template<class T>
int myCompare(const T &v1, const T &v2){
    cout << v1 << " " << ((v1 > v2) ? (">") : ((v1 < v2) ? ("<") : ("="))) << " " << v2 << endl;
    return (v1 > v2) ? (1) : ((v1 < v2) ? (-1) : 0);
};

int main(void) {
    int iRes;
    cout << "String 1 : " << "A"<<endl;
    cout << "String 2 : " << "a"<<endl;
    iRes = myCompare("A", "a");
    cout << "A " << ((iRes == 1) ? (">") : ((iRes == (-1)) ? ("<") : ("="))) << " a" << endl;
    return 0;
};
//----------------------------test.cpp end----------------


编译并运行它时出现问题:

在VS2008下,它提供:

String 1 : A
String 2 : a
A > a
A > a


在g ++(Debian 4.4.5-8)4.4.5下,它提供了:

String 1 : A
String 2 : a
A < a
A < a


正确答案是**A<a**

但是,当我注释掉以下内容时:

cout << "String 1 : " << "A" << endl;
cout << "String 2 : " << "a" << endl;


在VS2008下,它提供:

String 1 : A
String 2 : a
A < a
A < a


在g ++(Debian 4.4.5-8)4.4.5下,它提供了:

String 1 : A
String 2 : a
A > a
A > a


编译命令如下:

g++ test.cpp -o test -Wall -O0


我想知道为什么会这样吗?我使用不正确的问题(A?B:C)表达式是吗?看来我的代码是正确的。好几天困扰了我。任何帮助,将不胜感激。



结案!

我用以下代码替换了myCompare()调用:

iRes=myCompare(static_cast<string>("A"), static_cast<string>("a"));


有用。无论我如何更改上下文,它始终会给出正确的答案。我应该已经知道我正在比较两个const char*,因此请尝试一下以确保确定。

我在这里学到的教训是可以将该模板实例化为两个函数:

int compare(const char* &v1, const char* &v2);     // two pointers
int compare(const string &v1, const string &v2);   // two objects


感谢你们提供的所有帮助。非常感谢!祝你有美好的一天!



附加信息。

我在myCompare函数中添加了一个简单表达:

cout<<"the type is : "<<typeid(T).name() <<endl;


这提供了有关已实例化T的类​​型的明确类型信息。
整个代码如下:

//----------------------------test.cpp start----------------
#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

template<class T>
int myCompare(const T &v1, const T &v2){
    cout<<"the type is : "<<typeid(T).name() <<endl;
    cout<<v1<<" "<<((v1>v2)?(">"):((v1<v2)?("<"):("=")))<<" "<<v2<<endl;
    return (v1>v2)?(1):((v1<v2)?(-1):0);
};

int main(void){
    int iRes;
    iRes=myCompare(1234, 3);
    iRes=myCompare("test","poor");
    iRes=myCompare(static_cast<string>("test"),static_cast<string>("poor"));
    iRes=myCompare(21.23,4.0);
    return 0;
};

//----------------------------test.cpp end----------------


该程序的结果是:

the type is : i
1234 > 3
the type is : A5_c
test > poor
the type is : Ss
test > poor
the type is : d
21.23 > 4


注意:

1, #include <typeinfo> should be included.
2, the second comparison is incorrect, it simply compares the addresses of the constant char array rather than string stored in the constant char array.


希望这可以帮助像我一样在模板方面苦苦挣扎的人。

最佳答案

当前,您正在比较指针的值,而不是将const char *视为字符串,这几乎从来都不是您想要的。如果要比较const char *std::string,则应专门化模板。

template<>
int myCompare(const char *const &v1, const char *const &v2){
    return myCompare<std::string>(v1, v2);
};


或者,在这种情况下,请致电strcmp

template<>
int myCompare(const char *const &v1, const char *const &v2){
    return strcmp(v1, v2);
};

07-24 09:44
查看更多