C++ STL sort()函数用法

C++STL提供的在里的排序函数,有以下两种形式
此外还提供有稳定排序版本stable_sort(),用法类似.
第一种形式:

template <class RandomAccessIterator>
  void sort (RandomAccessIterator first, RandomAccessIterator last);

第二种形式:

template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

用法

firstlast是待排序序列的初始位置和终止位置

comp是比较函数
模板1没有比较函数,排序结果为升序,模板2可以根据比较函数comp实现升序还是降序

升序

bool comp(int a, int b){
return a < b ? true : false; //a >b返回true,不交换
}

降序

bool comp(int a, int b){
return a > b ? true : false; // a >b返回true,不交换
}

原文地址:https://www.cnblogs.com/zhuobo/p/10207762.html