别人给的C++,有一些地方看不懂...望大牛解释一下
#include <algorithm>
using namespace std;
struct ttype
{
char l[5];
int p;
} t[50001];
struct nodetype
{
int l,r,f;
} tr[50001];
int n;
bool cmp(ttype a,ttype b){return strcmp(a.l,b.l)<0;} //这句话是什么意思?是函数吗?又关ttype a,ttype b什么事?
void printans(int w)
省略...
void maketree()
省略...
int main()
{
省略...
sort(t+1,t+n+1,cmp); //这个sort是干嘛的?快速排序吗?cmp是什么?与之前那个cmp有关吗?
省略...
return 0;
}
之前有人回答过这样的话:
bool cmp(ttype a,ttype b) //定义一个函数cmp,a和b是作为这个函数的参数传入
{
return strcmp(a.l,b.l)<0; //cmp函数的函数体,比较a.l和b.l的值,如果a.l<b.l返回真,否则返回假。
}
sort(t+1,t+n+1,cmp);是调用sort函数,并将t+1、t+n+1和cmp的值作为参数。这里的cmp应该是定义的一个变量,与前面的cmp无关。
最后这句"
sort(t+1,t+n+1,cmp);是调用sort函数,并将t+1、t+n+1和cmp的值作为参数。这里的cmp应该是定义的一个变量,与前面的cmp无关。"
cmp没有再定义,肯定和cmp有关的,调用sort这个函数的参数有什么意义,能详细讲讲吗?