64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <algorithm>
using namespace std;
char *A[4];
int cmp(const char *a, const char *b) {
return strcmp(a, b);
}
int main()
{
A[1] = "hello";
A[2] = "my";
A[3] = "zzz";
A[0] = "world";
sort(A, A+4, cmp);
int i;
for(i=0; i<4; i++) cout << A[i] << endl;
return 0;
}
return strcmp(a, b)<0;
int cmp(const char *a, const char *b) {
return strcmp(a, b);
}
改成
bool cmp(const char *a, const char *b) {
return strcmp(a, b) <= 0;//<=都表示位置正确
}
bool cmp(const char *a, const char *b)
{
if( strcmp(a, b) < 0 )
return true;
else
return false;
}