C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等

 

上学时我们很多学了很多种排序算法,不过在c++stl中也封装了sort等函数,头文件是#include <algorithm>

image

 

函数名

功能描述

sort

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

stable_sort

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

partial_sort

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

partial_sort_copy

对给定区间复制并排序

nth_element

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

is_sorted

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

partition

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

stable_partition

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h"
#include <algorithm>
#include "iostream"
using namespace std;
 
int main(int argc, char* argv[])
{
    int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    sort(a,a+11);
    cout<<'\n';
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    return 0;
}

  

image

 

输出结果将是把数组a按升序排序,说到这里可能就有人会问怎么样用它降序排列呢?这就是下一个讨论的内容.
 
一种是自己编写一个比较函数来实现,接着调用第三个参数的sort:sort(begin,end,compare)就成了。

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

1)自己编写compare函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"
#include <algorithm>
#include "iostream"
using namespace std;
bool compare(int a,int b)
{
    return a>b;   //降序排列,如果改为return a<b,则为升序
}
 
 
int main(int argc, char* argv[])
{
    int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    sort(a,a+11,compare);
    cout<<'\n';
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    return 0;
}

  

image

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;
              }
          }
};

接下来使用 sort(begin,end,compare(ASC)实现升序,sort(begin,end,compare(DESC)实现降序。

完整代码为

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "stdafx.h"
#include <algorithm>
#include "iostream"
using namespace std;
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 argc, char* argv[])
{
    int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    sort(a,a+11,compare(ASC));
    cout<<'\n';
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    return 0;
}

  

image

 

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>()).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h"
#include <algorithm>
#include "iostream"
#include "functional"
using namespace std;
 
  
 
int main(int argc, char* argv[])
{
    int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    sort(a,a+11,greater<int>());
    cout<<'\n';
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    return 0;
}

  


image

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <algorithm>
#include "iostream"
#include "string"
using namespace std;
 
  
 
int main(int argc, char* argv[])
{
   string str="avfgrtty";
   int i;
    cout<<str<<'\n';
    for (string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
    cout << *rit;;
    cout<<endl;
    return 0;
}

  

image

 

qsort():快速排序算法
原型:
_CRTIMP void __cdecl qsort (void*, size_t, size_t,int (*)(const void*, const void*));

解释:    qsort ( 数组名 ,元素个数,元素占用的空间(sizeof),比较函数)
比较函数是一个自己写的函数  遵循 int com(const void *a,const void *b) 的格式。
当a b关系为 >  <  = 时,分别返回正值 负值 零 (或者相反)。
使用a b 时要强制转换类型,从void * 转换回应有的类型后,进行操作。
数组下标从零开始,个数为N, 下标0-(n-1)。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include <algorithm>
#include "iostream"
#include "string"
using namespace std;
 
int compare(const void *a,const void *b)
{
    return *(int*)b-*(int*)a;  
}
 
 
int main(int argc, char* argv[])
{
    int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    qsort((void *)a,11,sizeof(int),compare);
    cout<<'\n';
    for(i=0;i<11;i++)
        cout<<a[i]<<',';
    return 0;
 
}

  

image

 

 

 

相关:
1)why你必须给予元素个数?
因为阵列不知道它自己有多少个元素
2)why你必须给予大小?
因为 qsort 不知道它要排序的单位.
3)why你必须写那个丑陋的、用来比较俩数值的函式?
因为 qsort 需要一个指标指向某个函式,因为它不知道它所要排序的元素型别.
4)why qsort 所使用的比较函式接受的是 const void* 引数而不是 char* 引数?
因为 qsort 可以对非字串的数值排序.

 

以上实例是基于vc6.0的

posted @   Blue妞  阅读(11088)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示