用sort对二维数组排序

时间:2022-01-23 04:15:22

对二维字符串数组按字典序排序:strp[i]指向str每一行的首地址。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int MAXN = 100;

char str[MAXN][100];
char *strp[MAXN];

bool cmp(const char* s1,const char* s2)
{
return strcmp(s1,s2)<0;
}

int main()
{
//freopen("a.txt","r",stdin);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
strp[i]=str[i];
}
sort(strp,strp+n,cmp);
for(int i=0;i<n;i++)
{
printf("%s\n",strp[i]);
}
return 0;
}