关天std::sort的第三个参数。
请看下面这两个小程序:
//这个程序没有问题
#include <iostream>
#include <algorithm>
bool f(int a, int b)
{
return a > b;
}
int main(void)
{
int a[10] = {12, 6, 987, 45, 356, 1, 849, 32, 42, 58};
std::sort(a, a + 10, f);
return 0;
}
//这个程序不能运行(有Link错误)
#include <iostream>
#include <algorithm>
template <class T>
bool f(T a, T b)
{
return a > b;
}
int main(void)
{
int a[10] = {12, 6, 987, 45, 356, 1, 849, 32, 42, 58};
std::sort(a, a + 10, f<int>);
return 0;
}
第二个程序与第一个并没有太大的差别,可是为什么不行?