#include<cstring>
#include<iostream>
using namespace std;
template<class T>
void sort(T a[], int inf, int sup, bool(*f)(T, T))
{
T active = a[l];
int l = inf, r = sup;
while (l != r)
{
for (;r > l; --r)
if (f(a[l], a[r]))
{
a[l] = a[r];
++l;
}
for (; l < r; ++l)
if (f(a[l], a[r]))
{
a[r] = a[l];
--r;
}
}
a[l] = active;
sort(a[], inf, r-1, f);
sort(a[], l+1, sup, f);
}
int main()
{
int num[10] = { 123,345,645,2,26,74,234,65,3,4 };
char *txt[10] = { "wan","hua","cong","zhong","guo","pian","ye","bu","zhan","shen" };
sort(num, 0, 9, [](int x, int y)->bool {return x >= y; });
sort(txt, 0, 9, [](char *x, char *y)->bool {return strcmp(x, y) > 0; });
int i = 0;
for (i = 0; i < 10; ++i)
cout << num[i] <<'\t';
for (i = 0; i < 10; ++i)
cout << *txt[i] << '\t';
cout << endl;
system("pause");
return 0;
}
以上这是按照教材写的,实际编译时,sort函数处有红波浪线显示参数类型不匹配,编译器报错:
(41): error C2672: “sort”: 未找到匹配的重载函数
(41): error C2784: “void sort(T [],int,int,bool (__cdecl *)(T,T))”: 未能从“main::<lambda_e1f9132a44772aa5339c29c0560e8151>”为“bool (__cdecl *)(T,T)”推导 模板 参数
(11): note: 参见“sort”的声明
(42): error C2672: “sort”: 未找到匹配的重载函数
(42): error C2784: “void sort(T [],int,int,bool (__cdecl *)(T,T))”: 未能从“main::<lambda_02659c3c65205445e4171697753f100b>”为“bool (__cdecl *)(T,T)”推导 模板 参数
(11): note: 参见“sort”的声明
请大神指教如何修改!
3 个解决方案
#1
sort(a[], inf, r-1, f); 这是什么鬼,第一个参数怎么能是a[ ]?
不要无脑抄书上的代码,请理解以后再自己写
不要无脑抄书上的代码,请理解以后再自己写
#2
template<class T,class compare>
void sort(T a[], int inf, int sup, compare cmp){
}
#3
#include<cstring>
#include<iostream>
using namespace std;
template<class T>
void sort(T a[], int inf, int sup, bool(*f)(T, T))
{
T active = a[1];
int l = inf, r = sup;
while (l != r)
{
for (; r > l; --r)
if (f(a[l], a[r]))
{
a[l] = a[r];
++l;
}
for (; l < r; ++l)
if (f(a[l], a[r]))
{
a[r] = a[l];
--r;
}
}
a[l] = active;
sort(a, inf, r - 1, f);
sort(a, l + 1, sup, f);
}
int main()
{
int num[10] = { 123,345,645,2,26,74,234,65,3,4 };
char *txt[10] = { "wan","hua","cong","zhong","guo","pian","ye","bu","zhan","shen" };
sort<int>(num, 0, 9, [](int x, int y)->bool {return x >= y; });
sort<char*>(txt, 0, 9, [](char *x, char *y)->bool {return strcmp(x, y) > 0; });
int i = 0;
for (i = 0; i < 10; ++i)
cout << num[i] << '\t';
for (i = 0; i < 10; ++i)
cout << *txt[i] << '\t';
cout << endl;
system("pause");
return 0;
}
改变的是(1)sort函数里面的:
sort(a, inf, r - 1, f);
sort(a, l + 1, sup, f);
(2)主函数里面的:
sort<int>(num, 0, 9, [](int x, int y)->bool {return x >= y; });
sort<char*>(txt, 0, 9, [](char *x, char *y)->bool {return strcmp(x, y) > 0; });
#1
sort(a[], inf, r-1, f); 这是什么鬼,第一个参数怎么能是a[ ]?
不要无脑抄书上的代码,请理解以后再自己写
不要无脑抄书上的代码,请理解以后再自己写
#2
template<class T,class compare>
void sort(T a[], int inf, int sup, compare cmp){
}
#3
#include<cstring>
#include<iostream>
using namespace std;
template<class T>
void sort(T a[], int inf, int sup, bool(*f)(T, T))
{
T active = a[1];
int l = inf, r = sup;
while (l != r)
{
for (; r > l; --r)
if (f(a[l], a[r]))
{
a[l] = a[r];
++l;
}
for (; l < r; ++l)
if (f(a[l], a[r]))
{
a[r] = a[l];
--r;
}
}
a[l] = active;
sort(a, inf, r - 1, f);
sort(a, l + 1, sup, f);
}
int main()
{
int num[10] = { 123,345,645,2,26,74,234,65,3,4 };
char *txt[10] = { "wan","hua","cong","zhong","guo","pian","ye","bu","zhan","shen" };
sort<int>(num, 0, 9, [](int x, int y)->bool {return x >= y; });
sort<char*>(txt, 0, 9, [](char *x, char *y)->bool {return strcmp(x, y) > 0; });
int i = 0;
for (i = 0; i < 10; ++i)
cout << num[i] << '\t';
for (i = 0; i < 10; ++i)
cout << *txt[i] << '\t';
cout << endl;
system("pause");
return 0;
}
改变的是(1)sort函数里面的:
sort(a, inf, r - 1, f);
sort(a, l + 1, sup, f);
(2)主函数里面的:
sort<int>(num, 0, 9, [](int x, int y)->bool {return x >= y; });
sort<char*>(txt, 0, 9, [](char *x, char *y)->bool {return strcmp(x, y) > 0; });