浅谈C/C++排序函数中cmp()比较函数的写法(qsort sort函数)
2017-10-19 15:03
489 查看
转自 http://blog.csdn.net/lionel_d/article/details/41746135
首先,我们来谈谈大名鼎鼎的void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
它属于C语言标准库函数,应该是运用最多的了,今天我不是来教你们怎么用qsort的,只是来交流一下排序函数的最后一个参数cmp()(它不仅可以叫cmp,你还可以给他取名叫什么pig啊dog的只要是英文单词都可以,cmp只是人们对compare的一种常用缩写)比较函数的写法。
下面是cmp的写法:
[cpp] view
plain copy
int cmp(const void *a ,const void *b)
{
return *(int *)a - *(int *)b ; //从小到大排序,把a,b位置反过来就是从大到小
}
注意:qsort的cmp()函数千万别写成下面这样
[cpp] view
plain copy
//错错错错错错错 错错错错错错错 错错错错错错错 错错错错错错错
int cmp(const void *a ,const void *b)
{
return *(int *)a > *(int *)b ; // > 与 < 都不行 !
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
有一次就是写了下面的cmp(),结果排序死活不对!
下面是完整的测试代码:
[cpp] view
plain copy
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a ,const void *b)
{
return *(int *)a - *(int *)b ; //从小到大排序,把a,b位置反过来就是从大到小,
要加上*号
}
int main()
{
int a[10]={-1,9,5,7,-11,2,6,8,9,6};
qsort(a,10,sizeof(int),cmp);
for(int i = 0 ; i < 10 ; ++i)
{
printf("%d ",a[i]);
}
printf("\n") ;
return 0 ;
}
测试结果:
第二个cmp(),就是void
sort( iterator start, iterator end, StrictWeakOrdering cmp );下面是标准声明:
他的头文件是<algorithm>,这个是标准C++头文件里的。
[cpp] view
plain copy
bool cmp(int a ,int b)
{
return a < b ; //从小到大排序,把 < 换成 > 就是从大到小
}
好的,问题又来了,这个cmp与qsort的cmp不一样了,正好相反,他不能写成下面这样:
[cpp] view
plain copy
//错错错错错错错 错错错错错错错 错错错错错错错 错错错错错错错
bool cmp(int a ,int b)
{
return a - b ;
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
切记,切记!
下面是sort的测试代码:
[cpp] view
plain copy
#include <iostream>
#include <algorithm>
using namespace std ;
bool cmp(int a ,int b)
{
return a < b ; //从小到大排序,把 < 换成 > 就是从大到小
}
int main()
{
int a[10]={-1,9,5,7,-11,2,6,8,9,6};
sort(a,a+10,cmp);
for(int i = 0 ; i < 10 ; ++i)
{
cout<<a[i]<<" " ;
}
cout<<endl ;
return 0 ;
}
测试结果:
在C++中,我们经常需要用到set,map等容器,他们的cmp基本写法都与sort的相同,当然set,map的cmp可不仅仅是函数了,而是函数对象:
[cpp] view
plain copy
struct cmp{
bool operator ()(const int a , const int b)
{
return a < b ; // 从小到大,反过来就是从大到小
}
};
下面仅仅对set做代码测试:
[cpp] view
plain copy
#include <iostream>
#include <cstring>
#include <set>
using namespace std ;
struct Person{
int age;
char name[20];
Person(int Age , const char Name[]):age(Age){strcpy(name,Name);}
};
struct cmp{
bool operator ()(const Person a , const Person b)
{
return a.age < b.age ; // 从小到大 ;
}
};
int main()
{
set<Person,cmp> s ;
Person n1(46,"ggg");
Person n2(-16,"fff"); //年龄无负数,只是为了测试代码,下同
Person n3(45,"eee");
Person n4(-25,"ddd");
Person n5(34,"ccc");
Person n6(22,"bbb");
Person n7(2,"aaa");
s.insert(n1);
s.insert(n2);
s.insert(n3);
s.insert(n4);
s.insert(n5);
s.insert(n6);
s.insert(n7);
set<Person,cmp>::iterator begin = s.begin();
set<Person,cmp>::iterator end = s.end();
for(set<Person,cmp>::iterator i = begin ; i != end ; ++i)
{
cout<<i->age<<" "<<i->name<<endl ;
}
return 0 ;
}
测试结果:
首先,我们来谈谈大名鼎鼎的void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
它属于C语言标准库函数,应该是运用最多的了,今天我不是来教你们怎么用qsort的,只是来交流一下排序函数的最后一个参数cmp()(它不仅可以叫cmp,你还可以给他取名叫什么pig啊dog的只要是英文单词都可以,cmp只是人们对compare的一种常用缩写)比较函数的写法。
下面是cmp的写法:
[cpp] view
plain copy
int cmp(const void *a ,const void *b)
{
return *(int *)a - *(int *)b ; //从小到大排序,把a,b位置反过来就是从大到小
}
注意:qsort的cmp()函数千万别写成下面这样
[cpp] view
plain copy
//错错错错错错错 错错错错错错错 错错错错错错错 错错错错错错错
int cmp(const void *a ,const void *b)
{
return *(int *)a > *(int *)b ; // > 与 < 都不行 !
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
有一次就是写了下面的cmp(),结果排序死活不对!
下面是完整的测试代码:
[cpp] view
plain copy
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a ,const void *b)
{
return *(int *)a - *(int *)b ; //从小到大排序,把a,b位置反过来就是从大到小,
要加上*号
}
int main()
{
int a[10]={-1,9,5,7,-11,2,6,8,9,6};
qsort(a,10,sizeof(int),cmp);
for(int i = 0 ; i < 10 ; ++i)
{
printf("%d ",a[i]);
}
printf("\n") ;
return 0 ;
}
测试结果:
第二个cmp(),就是void
sort( iterator start, iterator end, StrictWeakOrdering cmp );下面是标准声明:
template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
他的头文件是<algorithm>,这个是标准C++头文件里的。
[cpp] view
plain copy
bool cmp(int a ,int b)
{
return a < b ; //从小到大排序,把 < 换成 > 就是从大到小
}
好的,问题又来了,这个cmp与qsort的cmp不一样了,正好相反,他不能写成下面这样:
[cpp] view
plain copy
//错错错错错错错 错错错错错错错 错错错错错错错 错错错错错错错
bool cmp(int a ,int b)
{
return a - b ;
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
切记,切记!
下面是sort的测试代码:
[cpp] view
plain copy
#include <iostream>
#include <algorithm>
using namespace std ;
bool cmp(int a ,int b)
{
return a < b ; //从小到大排序,把 < 换成 > 就是从大到小
}
int main()
{
int a[10]={-1,9,5,7,-11,2,6,8,9,6};
sort(a,a+10,cmp);
for(int i = 0 ; i < 10 ; ++i)
{
cout<<a[i]<<" " ;
}
cout<<endl ;
return 0 ;
}
测试结果:
在C++中,我们经常需要用到set,map等容器,他们的cmp基本写法都与sort的相同,当然set,map的cmp可不仅仅是函数了,而是函数对象:
[cpp] view
plain copy
struct cmp{
bool operator ()(const int a , const int b)
{
return a < b ; // 从小到大,反过来就是从大到小
}
};
下面仅仅对set做代码测试:
[cpp] view
plain copy
#include <iostream>
#include <cstring>
#include <set>
using namespace std ;
struct Person{
int age;
char name[20];
Person(int Age , const char Name[]):age(Age){strcpy(name,Name);}
};
struct cmp{
bool operator ()(const Person a , const Person b)
{
return a.age < b.age ; // 从小到大 ;
}
};
int main()
{
set<Person,cmp> s ;
Person n1(46,"ggg");
Person n2(-16,"fff"); //年龄无负数,只是为了测试代码,下同
Person n3(45,"eee");
Person n4(-25,"ddd");
Person n5(34,"ccc");
Person n6(22,"bbb");
Person n7(2,"aaa");
s.insert(n1);
s.insert(n2);
s.insert(n3);
s.insert(n4);
s.insert(n5);
s.insert(n6);
s.insert(n7);
set<Person,cmp>::iterator begin = s.begin();
set<Person,cmp>::iterator end = s.end();
for(set<Person,cmp>::iterator i = begin ; i != end ; ++i)
{
cout<<i->age<<" "<<i->name<<endl ;
}
return 0 ;
}
测试结果:
相关文章推荐
- sort()/qsort()函数的用法及cmp比较函数的写法
- JAVA中sort函数的 自定义排序 cmp函数的写法 (基础向)
- C/C++ 调用qsort/sort 对字符数组排序的cmp函数写法
- 字符串数组排序(qsort参数 比较函数)
- qsort中cmp函数写法汇总
- 关于qsort的比较函数的不同写法对于大数据造成的错误
- 有关排序函数cmp写法
- LeetCode/sort(地址A,地址B)和qsort(首地址,元素个数,sizeof(),cmp比较函数)的使用
- poj 2075 lightblueme double qsort cmp函数的写法
- 浅谈C/C++排序函数中cmp()比较函数的写法
- 【C/C++】C语言中qsort的cmp函数写法汇总
- 我想用strcmp() 作为比较函数, 调用qsort() 对一个字符串数组排序, 但是不行。
- qsort()中cmp函数写法
- 浅谈C/C++排序函数中cmp()比较函数的写法
- qsort和sort函数中的 cmp函数
- 浅谈C/C++排序函数中cmp()比较函数的写法
- C++中 qsort中cmp函数写法汇总
- 谈谈C/C++中的排序函数的比较函数的写法。切忌不要走火入魔。
- c语言标准库低通的qsort函数不适宜所有排序任务的原因
- lexicographical_compare()按字典序比较函数用法示例(字符串排序)