sort函數——如何降序排序


sort函數默認是進行升序排序,有兩種方式可以進行降序。

  • 使用greater<int>()
sort(arr, arr + 5, greater<int>());
  • 自定義一個比較大小的函數,將大的排前面
bool cmp(int x,int y){
	return x > y;
}
sort(arr,arr + 5,cmp);

reference:https://blog.csdn.net/lytwy123/article/details/84503492


免責聲明!

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



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