排序

头文件都是:#include<algorithm>

qsort篇

 qsort(a,len,sizeof(type),mycmp)

//注释

a:待排序的数组地址

len:待排序的数组的长度

type:待排序的数组元素的类型

mycmp:自定义的排序函数

View Code
 1 /****************一维数组类比较函数************
 2 int mycmp(const void *a,const void *b)
 3 {
 4  return *(int *)a - *(int *)b;
 5 }
 6 //注 char、float、double型数组时
 7 //只需将return中的int改成相应的类型即可。
 8 //(const void *a,const void *b)必须照写
 9 ********************************************/
10 
11 
12 /****************字符串类比较函数(二维)************
13 int mycmp(const void *a,const void *b)
14 {
15  return strcmp((char *)a,(char *)b);
16 }
17 ************************************************/
18 /*************二维数组整体排序比较函数******
19 int mycmp(const void *a,const void *b)
20 {
21  return *(int *)a-*(int *)b;
22  //注 char、float、double型数组时
23 //只需将return中的int改成相应的类型即可。
24 //(const void *a,const void *b)必须照写
25 }
26 ****************************************/
27 /*************一维结构体排序比较函数******
28     struct  node
29     {
30      int a;
31     };
32 int mycmp(const void *c,const void *b)
33 {
34  return (*(node *)c).a-(*(node *)b).a;
35 }
36 *********************************************/
37 /*****************二维结构体排序*********************
38 struct node 
39 {
40  int data;
41  char str[5];
42 };
43 int mycmp(const void *a,const void *b)
44 {
45   if((*(node *)a).data!=(*(node *)b).data)
46       return  (*(node *)a).data -(*(node *)b).data ;
47   return strcmp((*(node *)a).str,(*(node *)b).str );
48  
49 }
50 *******************************************************/
51 
52 int main()
53 {
54     //qsort篇
55   /*************一维数组类**************
56     int a[7]={2,1,3,5,4,7,6},i;
57      qsort(a,7,sizeof(int),mycmp);
58      for(i=0;i<7;i++)
59      cout<<a[i]<< "  ";
60      cout<<endl;
61   **************************************/
62 
63   
64   
65   /**************字符串(二维)******************
66     char s[5][4]={"aa","dd","cc","ee","bb"};
67      qsort(s,5,sizeof(s[0]),mycmp);
68      for(int i=0;i<5;i++)
69          cout<<s[i]<<endl;
70 ***********************************************/
71 /**************二维数组******************    
72     int a[7][2]={{2,2},{1,9},{3,3},{5,5},{4,4},{7,7},{6,6}},i;
73      qsort(a,7,sizeof(a[0]),mycmp);//分组排序
74 //     qsort(a,14,sizeof(int),mycmp);//全部元素一起排序,mycmp为一维int
75      for(i=0;i<7;i++)
76          for(int j=0;j<2;j++)
77      cout<<a[i][j]<< "  ";
78      cout<<endl;
79 *************************************************/
80 /***********************一维结构体排序*************
81 node b[5];
82 b[0].a=5;
83 b[1].a=3;
84 b[2].a=2;
85 b[3].a=4;
86 b[4].a=1;
87 qsort(b,5,sizeof(node),mycmp);
88 for(int i=0;i<5;i++)
89 cout<<b[i].a <<endl;
90 *************************************************/
91  
92 /*******************二维结构体排序****************
93     node s[5]={{5,"dd"},{3,"dd"},{5,"cc"},{1,"aa"},{2,"bb"}};    
94  qsort(s,5,sizeof(node),mycmp);
95  for(int i=0;i<5;i++)
96      cout<<s[i].data<<" "<<s[i].str<<endl;  
97 *****************************************/

 sort篇

 sort(a,a+n,mycmp)

a:待排序的数组的起始地址

a+n:待排序数组的结束地址,n元素的个数

mycmp:自定义排序函数,省略则为默认升序

View Code
 1 /*************一维数组**********************
 2 bool mycmp(int a,int b)
 3 {
 4  return a>b;
 5 }
 6 //char、double、float只需要将int改为相应的类型即可
 7 ***************************************/
 8 /**************结构体******************
 9 struct node 
10 {
11  int data;
12  char str[5];
13 } ;
14 bool mycmp(node c,node b)
15 {
16  if(c.data!=b.data )return c.data>b.data ;
17  return strcmp(c.str ,b.str)<0 ;
18 }
19 *****************************************/
20 int main()
21 {
22     //sort篇
23   /*************一维数组类**************
24     int a[7]={2,1,3,5,4,7,6},i;
25      sort(a,a+7,mycmp);
26      for(i=0;i<7;i++)
27      cout<<a[i]<< "  ";
28      cout<<endl;
29   **************************************/
30 /*******************结构体排序****************
31     node s[5]={{5,"dd"},{3,"dd"},{5,"cc"},{1,"aa"},{2,"bb"}};    
32  sort(s,s+5,mycmp);
33  for(int i=0;i<5;i++)
34      cout<<s[i].data<<" "<<s[i].str<<endl; 
35 //注:     二维数组的排序可以转化为结构体的排序
36 ************************************************/

注以下为map、set、priority_queue等的重载运算符的方法

 

View Code
 1 //元素不是结构体,键值从小到大
 2 struct mycmp
 3 {
 4   bool operator()(const int &a,const int &b)
 5   {
 6     if(a!=b)return a<b;
 7     else
 8         return a>b;
 9   }
10 };
11 //元素是结构体,可直接将比较函数写在结构体中
12 struct node
13 {
14 
15  string name;
16  int data;
17  bool operator < (const node &a)
18  {
19   return a.data<data;
20  //按data升序
21  }
22 };
23 //注:优先队列,元素为结构体时也可以通过重载"()"方式定义优先级

 优先队列的用法:

View Code
  1 转自:
  2 http://www.cppblog.com/shyli/archive/2007/04/06/21366.html
  3 在优先队列中,优先级高的元素先出队列。
  4 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
  5 优先队列的第一种用法,也是最常用的用法:
  6 
  7 priority_queue<int> qi;
  8 通过<操作符可知在整数中元素大的优先级高。
  9 故示例1中输出结果为:9 6 5 3 2
 10 
 11 第二种方法:
 12 在示例1中,如果我们要把元素从小到大输出怎么办呢?
 13 这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。
 14 
 15 priority_queue<int, vector<int>, greater<int> >qi2;
 16 其中
 17 第二个参数为容器类型。
 18 第二个参数为比较函数。
 19 故示例2中输出结果为:2 3 5 6 9
 20 
 21 第三种方法:
 22 自定义优先级。
 23 
 24 struct node
 25 {
 26     friend bool operator< (node n1, node n2)
 27     {
 28         return n1.priority < n2.priority;
 29     }
 30     int priority;
 31     int value;
 32 };
 33 在该结构中,value为值,priority为优先级。
 34 通过自定义operator<操作符来比较元素中的优先级。
 35 在示例3中输出结果为:
 36 优先级  值
 37 9          5
 38 8          2
 39 6          1
 40 2          3
 41 1          4
 42 但如果结构定义如下:
 43 
 44 struct node
 45 {
 46     friend bool operator> (node n1, node n2)
 47     {
 48         return n1.priority > n2.priority;
 49     }
 50     int priority;
 51     int value;
 52 };
 53 则会编译不过(G++编译器)
 54 因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
 55 而且自定义类型的<操作符与>操作符并无直接联系,故会编译不过。
 56 
 57 //代码清单
 58 
 59 #include<iostream>
 60 #include<functional>
 61 #include<queue>
 62 using Namespace stdnamespace std;
 63 struct node
 64 {
 65     friend bool operator< (node n1, node n2)
 66     {
 67         return n1.priority < n2.priority;
 68     }
 69     int priority;
 70     int value;
 71 };
 72 int main()
 73 {
 74     const int len = 5;
 75     int i;
 76     int a[len] = {3,5,9,6,2};
 77     //示例1
 78     priority_queue<int> qi;
 79     for(i = 0; i < len; i++)
 80         qi.push(a[i]);
 81     for(i = 0; i < len; i++)
 82     {
 83         cout<<qi.top()<<" ";
 84         qi.pop();
 85     }
 86     cout<<endl;
 87     //示例2
 88     priority_queue<int, vector<int>, greater<int> >qi2;
 89     for(i = 0; i < len; i++)
 90         qi2.push(a[i]);
 91     for(i = 0; i < len; i++)
 92     {
 93         cout<<qi2.top()<<" ";
 94         qi2.pop();
 95     }
 96     cout<<endl;
 97     //示例3
 98     priority_queue<node> qn;
 99     node b[len];
100     b[0].priority = 6; b[0].value = 1; 
101     b[1].priority = 9; b[1].value = 5; 
102     b[2].priority = 2; b[2].value = 3; 
103     b[3].priority = 8; b[3].value = 2; 
104     b[4].priority = 1; b[4].value = 4; 
105 
106     for(i = 0; i < len; i++)
107         qn.push(b[i]);
108     cout<<"优先级"<<'\t'<<""<<endl;
109     for(i = 0; i < len; i++)
110     {
111         cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
112         qn.pop();
113     }
114     return 0;
115 }

 

 

 

posted on 2012-07-26 10:45  L_S_X  阅读(206)  评论(0编辑  收藏  举报

导航