sort 函數是經常要用到的進階函數,用到好處,你會感覺你真的很棒!
但是,第一個問題就是,你十分了解sort函數嗎?其實不然。如果你就不徹底了解它,怎能用好呢?
現在我就小漏一手吧!
我們可以自定義sort函數呢,當然,它要和結構體共同使用,那樣更爽,它的這個功能主要用在多級排序上,友善死了。
例如:
struct student
{
char name;
int x, y, z;
}stu[1000];
bool check(strdent a, student b)
{
if(a.x > b.x)return true;
if(a.x == b.x && a.y > b.y) return true;
return false;
}
int main()
{
int n;
cin>>n;
for(int i=0; i<n; i++)
cin>>stu[i].name>>stu[i].x>>stu[i].y>>stu[i].z;
sort(stu, stu+n, check)
.........
}
怎麼樣,有點收獲吧。
當然,我所知道是有限的,不可能很全面,還望高手指點幾招。