algorithm库介绍 sort()方法

algorithm库介绍 sort()方法

sort()方法用来将指定范围内的元素进行排序,默认是按照升序排列,也可以自定义比较函数 comp...

sort有两个重载:

void sort ( RandomAccessIterator first, RandomAccessIterator last );

void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

参数说明:

Parameters

first, last  第一,第二个参数给定了需要排序的范围 [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.
comp 第三个参数指定了自定义的比较函数, 默认是按照operator<()即按照升序来排序的。 自定义时,可以使用function来定义排序规则,也可以自定义struct(在struct中必须对 operator()进行重写)
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.

Return value  无返回值

none

 

举例说明:

#include <iostream>
 #include <vector>
 #include <algorithm>
 
 using namespace std;
 
 bool comp(int i, int j)
 {
     return (i<j);
 }
 
 struct myclass
 {
     bool operator()(int i, int j){return (i<j);}
 
 } myobject;
 
 int main()
 {
     int myints[] = {50, 60, 30, 25, 20, 10, 80, 40};
     vector<int> v(myints, myints + 8);
     vector<int>::iterator it;   
     cout<< "the original array is: "<<endl;
     for(int i = 0; i< (sizeof(myints)/sizeof(int)); i++)
         cout<< myints[i] << " ";
     cout<<endl;  
 
     //use default comp to sort
     sort (v.begin(), v.begin()+4);
     cout<< "sort the first 4 elements with < ordering: "<<endl;
     for(it = v.begin(); it != v.end(); it++)
         cout<<*it<< " ";
     cout<<endl;
 
     //use the selfdefined comp function to sort the last four elements
     sort(v.begin()+4, v.end(), comp);
     cout<< "sort the last 4 elements with the selfdefined function comp: "<<endl;
     for(it = v.begin(); it != v.end(); it++)
         cout<<*it<< " ";
     cout<<endl;
 
     //use the struct function to sort, in which the operator() is overriding...
     sort(v.begin(), v.end(), myobject);
     cout<< "sort all of the 8 elements with the selfdefined struct myobject, in which the operator() is overriding:"<<endl;
     for(it = v.begin(); it != v.end(); it++)
         cout<<*it<< " ";
     cout<<endl;
    
     return 0;
 }

 

结果如下:

the original array is:
 50 60 30 25 20 10 80 40
 sort the first 4 elements with < ordering:
 25 30 50 60 20 10 80 40
 sort the last 4 elements with the selfdefined function comp:
 25 30 50 60 10 20 40 80
 sort all of the 8 elements with the selfdefined struct myobject, in which the operator() is overriding:
 10 20 25 30 40 50 60 80

 

转自:http://www.cnblogs.com/ffhajbq/archive/2012/07/24/2607342.html

自己验证了下第一重重载:

 int myints[] = {50, 60, 30, 25, 20, 10, 80, 40};
 vector<int> v(myints, myints + 8);
 vector<int>::iterator it;
 sort(v.begin(), v.end());
    for(it = v.begin(); it != v.end(); it++)
        cout<<*it<< " ";
    cout<<endl;

可以使用(别忘了头文件)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值