牛骨文教育服务平台(让学习变的简单)
博文笔记

c++ 中 sort 与 c 的去sort cmp 比较函数 书写区别,待总结

创建时间:2012-05-24 投稿人: 浏览次数:3360

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
/* int cmp( const int &a, const int &b ){
    if( a > b )
       return 1;
    else
       return 0;
} */
int main()
{
  
int cmp(const int &a,const int &b);
    int a[10]={5,3,4,6,7,3,2,3,74,6};
    sort(a,a+10,cmp);
    for(int i=0;i<=9;i++)
    printf("%d ",a[i]);
   
   
   
    system("pause");
    return 0;
}


// sort(a,a+n,cmp);

int cmp(const int &a,const int &b)
{return a-b;
//int i=*(int*)a,j=*(int*)b;
//if(i<j)return 1;else return -1;
    //if((*(int*)a)>(*(int*)b))return 1;
    //else return -1;
}

 

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。