int strcmp( char* str1,char* str2)
str1和str2可以是字符串常量或者字符串变量
①str1小于str2,返回-1;②str1等于str2,返回0;③str1大于str2,返回 1;
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct Student{
char name[20];
int id;
double gpa;
};
Student students[] = {
{ "Jack", 112, 3.4 }, { "Mary", 102, 3.8 }, { "Mary",117,3.9 },
{ "Ala", 333, 3.5 }, { "Zero",101,4.0 }
};
struct Rule1{ //按姓名从小到大排序
bool operator()(const Student&s1, const Student&s2){
if (strcmp(s1.name, s2.name) < 0)
return true;
return false;
}
};
void Print(Student a[], int size){
for (int i = 0; i < size; i++)
cout << a[i].name << " "<<a[i].id<<" "<<a[i].gpa;
cout << endl;
}
int main(){
int num = sizeof(students) / sizeof(Student);
sort(students, students + num, Rule1()); //按姓名从小到大排序
Print(students, num);
cin.get();
return 0;
}