C/C++中的sort()函数,,,,,,,

105 篇文章 1 订阅

sort()函数是C++中的排序函数其头文件为:#include<algorithm>头文件;

qsort()是C中的排序函数,其头文件为:#include<stdlib.h>

 

1、sort()

sort 对给定区间所有元素进行排序

stable_sort 对给定区间所有元素进行稳定排序

partial_sort 对给定区间所有元素部分排序

partial_sort_copy 对给定区间复制并排序

nth_element 找出给定区间的某个位置对应的元素

is_sorted 判断一个区间是否已经排好序

partition 使得符合某个条件的元素放在前面

stable_partition 相对稳定的使得符合某个条件的元素放在前面

 

语法描述为:

(1)sort(begin,end),表示一个范围,例如:

#include "stdafx.h"

#include<iostream>

#include <algorithm>

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

      int a[10]={2,4,1,23,5,76,0,43,24,65},i;

      for(i=0;i<10;i++)

           cout<<a[i]<<" ";

      cout<<endl;

      sort(a,a+5);

      for(i=0;i<10;i++)

           cout<<a[i]<<" ";

      system("pause");

      return 0;

}

输出结果将是把数组a按升序排序,说到这里可能就有人会问怎么样用它降序排列呢?这就是下一个讨论的内容。

(2)sort(begin,end,compare)

一种是自己编写一个比较函数来实现,接着调用三个参数的sort:sort(begin,end,compare)就成了。

对于list容器,这个方法也适用,把compare作为sort的参数就可以了,即:sort(compare)

 

1)自己编写compare函数:

bool compare(int a,int b)

{

      return a<b; //按升序排序,如果按降序排序改为“a>b”

}

int _tmain(int argc, _TCHAR* argv[])

{

      int a[10]={2,4,1,23,5,76,0,43,24,65},i;

      for(i=0;i<10;i++)

           cout<<a[i]<<" ";

      cout<<endl;

      sort(a,a+10,compare);

      for(i=0;i<10;i++)

           cout<<a[i]<<" ";

      system("pause");

      return 0;

}

 

2)更进一步,让这种操作更加能适应变化。也就是说,能给比较函数一个参数,用来指示是按升序还是按降序排,这回轮到函数对象出场了。

为了描述方便,我先定义一个枚举类型EnumComp用来表示升序和降序。很简单:

enum Enumcomp{ASC,DESC};

 

class compare

{

private:

      Enumcomp comp;

public:

      compare(Enumcomp c):comp(c) {};

      bool operator () (int num1,int num2)

      {

           switch(comp)

           {

           case ASC:

                 return num1<num2;

           case DESC:

                 return num1>num2;

           }

      }

};

 

int main()

{

      int a[20]={2,4,1,23,5,76,0,43,24,65},i;

      for(i=0;i<10;i++)

           cout<<a[i]<<" ";

      cout<<endl;

      sort(a,a+20,compare(DESC));

      for(i=0;i<10;i++)

           cout<<a[i]<<" ";

      return 0;

}

 

 

3)其实对于这么简单的任务(类型支持“<”、“>”等比较运算符),完全没必要自己写一个类出来。标准库里已经有现成的了,就在functional里,include进来就行了。functional提供了一堆基于模板的比较函数对象。它们是(看名字就知道意思了):equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>。对于这个问题来说,greater和less就足够了,直接拿过来用:

 

升序:sort(begin,end,less<data-type>());

降序:sort(begin,end,greater<data-type>()).

#include "stdafx.h"

#include<iostream>

#include <algorithm>

#include <xfunctional>

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

      int a[10]={2,4,1,23,5,76,0,43,24,65},i;

      for(i=0;i<10;i++)

           cout<<a[i]<<" ";

      cout<<endl;

 

      sort(a,a+10,greater<int>());

 

      for(i=0;i<10;i++)

           cout<<a[i]<<" ";

      system("pause");

      return 0;

}

 

int main()

{

      vector <int> vec;

      int a,i=0;

      cin>>a;

      while(cin.peek()!='\n')

      {

           vec.push_back(a);

           cin>>a;

      }

      vec.push_back(a);

 

      sort(vec.begin(),vec.end(),greater<int>());

 

      while(i<vec.size())

           cout<<vec[i++]<<" ";

 

      system("pause");

      return 0;

}

 

4)既然有迭代器,如果是string 就可以使用反向迭代器来完成逆序排列,程序如下:

#include "stdafx.h"

#include<iostream>

#include<string>

#include <algorithm>

using namespace std;

 

 

int main()

{

      string str("wanjun");

      string s(str.rbegin(),str.rend());

      cout<<s<<endl;

      system("pause");

      return 0;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值