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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM