为什么 sort() 的自定义比较函数无需参数即可工作(c++)?
void show(int a[])
{
cout<<endl;
for(int i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
}
bool compare(int a,int b)
{
return a>b;
}
int main()
{
int a[10]={12,32,45,22,643,53,53,32,4,32};
sort(a,a+10,compare);
show(a);
return 0;
}
谢谢
它不会“没有参数地工作”。你正在路过std::sort
指向该函数的指针。std::sort
然后将调用它(with参数)根据需要对范围进行排序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)