【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);
}

 

posted @ 2019-11-11 17:21  王牌飞行员_里海  阅读(5231)  评论(1编辑  收藏  举报