C++中Vector排序

1.普通类型(由大到小排序)

int main()
{
    sort(v.begin(),v.end());
}

2.普通类型(由小到大排序)

bool comp(const int &a,const int &b)
{
    return a>b;
}
int main()
{
    sort(v.begin(),v.end(),comp);
}

3.结构体类型

struct student
{
     char name[10];
     int score;
};

bool comp(const student &a, const student &b)
{
    return a.score < b.score; //由小到大排序
}
 
int main()
{
    vector<student> vectorStudents;
    sort(vectorStudents.begin(),vectorStudents.end(),comp);
}

猜你喜欢

转载自www.cnblogs.com/KMould/p/11836886.html
今日推荐