C++sort函数的用法

MSDN中的定义:

template<class RanIt>    
void sort(RanIt first, RanIt last); //--> 1) t
emplate<class RanIt, class Pred>    
void sort(RanIt first, RanIt last, Pred pr); //--> 2)
Sorts the elements in the range [first,last) into ascending order.
The elements are compared using operator< for the first version, and comp for the second.
Equivalent elements are not guaranteed to keep their original relative order (see stable_sort).
<pre name="code" class="cpp">[first,last)
Random-access iterators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
RandomAccessIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable.


 
 
 头文件: 
#include <algorithm>
using namespace std;

1.默认的sort函数是按升序排。对应于1)
sort(a,a+n); //两个参数分别为待排序数组的首地址和尾地址
2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)
例如:
int cmp( const int &a, const int &b ){
if( a > b )
return 1;
else
return 0;
}
sort(a,a+n,cmp);
是对数组a降序排序
又如:
int cmp( const POINT &a, const POINT &b ){
if( a.x < b.x )
return 1;
else
if( a.x == b.x ){
if( a.y < b.y )
return 1;
else
return 0;
}
else
return 0;
}

sort(a,a+n,cmp);
是先按x升序排序,若x值相等则按y升序排



与此类似的还有C中的qsort,以下同附上qsort的使用方法:

#include <stdlib.h>
格式 qsort(array_name,data_number,sizeof(data_type),compare_function_name) (void*)bsearch (pointer_to_key_word,array_name,find_number,
sizeof(data_type),compare_function_name)
e.g.
int Cmp(const void*a,const void *b)
{
int*pa=(int*)a,*pb=(int*)b;
if(*pa>*pb) return 1;
else if (*pa==*pb) return 0;
else return -1;
}
qsort(data,N,sizeof(int),Cmp); // 对int型数组进行快速排序(非降序排列)
p=(int*)bsearch(&a,data,n,sizeof(int),Cmp);

#include <iostream>
#include <algorithm> //这个是包含sort函数的头文件

using namespace std;

int arr[100], n;
int cmp(int x, int y){  //这个函数是sort函数比较两个元素优先级的函数,在这里计算出两个元素的优先级,然后返回即可

    //此处插入代码计算x,y的重要性
    return x < y; //此处返回小于号,意思是优先级小的在前面,如return x<y; 则数组会由小达到排序
}

int main(){
    cin>>n;
    for(int i = 0; i < n; i++)
        cin>>arr[i];
    sort(arr, arr + n, cmp);
    cout<<"after sort: "<<endl;
    for(int i = 0; i < n; i++)
        cout<<arr[i]<<" ";

    return 0;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++标准库中的`std::sort`函数是用于对容器(如`std::vector`, `std::array`, 或自定义容器)中的元素进行排序的成员函数。它使用快速排序算法(通常有优化版本),对元素进行升序排列。`sort`函数的基本用法如下: ```cpp template <class RandomIt> void sort(RandomIt first, RandomIt last); ``` 这里的`first`参数是要排序的范围的第一个元素的迭代器,`last`参数是排序范围的结束位置(不包含该元素)。 如果容器中的元素是非可比较的,或者需要自定义排序规则,可以提供一个比较函数或三元操作符作为第二个参数: ```cpp template <class RandomIt, class Compare> void sort(RandomIt first, RandomIt last, Compare comp); ``` 或者 ```cpp template <class RandomIt> void sort(RandomIt first, RandomIt last, bool (*pred)(RandomIt, RandomIt)); ``` 其中,`Compare`是一个对象,或者是满足`bool Compare(const T& a, const T& b)`签名的函数指针,用来指定元素间的比较规则;`pred`是一个返回`bool`值的函数指针,接受两个迭代器作为参数,表示它们所指向的元素之间的关系。 例子: ```cpp std::vector<int> v = {5, 2, 9, 1, 5, 6}; std::sort(v.begin(), v.end()); // 对vector进行默认升序排序 // 自定义排序 std::sort(v.begin(), v.end(), std::greater<int>()); // 降序排序 // 使用比较函数 bool compare(const int& a, const int& b) { return a > b; } std::sort(v.begin(), v.end(), compare); // 按照自定义规则排序 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值