今天做stdlib 中的QList 排序 ,在QT环境下 马上想到了qsort 可是呢 qsort用了半天发现不可行
功 能: 使用
快速排序例程进行排序
用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
其中的那个比较函数,传入的参数是个void*的而且转换完成后还是无法排序 ,最终发现用qsort对泛型排序真不是个好主意
接着用标准库中<algorithm>的sort排序 ,这个是一个专门针对泛型数据排序的中可以吧 ,可是写在qt中却无法识别 sort、std::sort 都试过了
Header: <algorithm> Namespace: std
最后找下Qt中的专用排序,发现Qlist 中的排序 用的是qsort ,郁闷,可以,,于是便又在琢磨 ,好久,发现不行,
继续查阅资料 发现一个qSort,Qt中对泛型排序的函数,发现和sort基本上完全相同 ,这时才醒悟过来 qsort -----qSort
void qSort ( RandomAccessIterator begin, RandomAccessIterator end, LessThan lessThan )
void qSort ( RandomAccessIterator begin, RandomAccessIterator end )
- bool CapitySort(const SVideoChip msVideoFirst,const SVideoChip msVideoSecond)
- {
- return (msVideoFirst.mi64VideoCapacity < msVideoSecond.mi64VideoCapacity);
- }
- void * VideoSort(QList<SVideoChip>* msVideoChipList)
- {
- qSort(msVideoChipList->begin(),msVideoChipList->end(),CapitySort);
- // std::sort(msVideoChipList->begin(),msVideoChipList->end(),CapitySort);
- }
- bool CapitySort(const SVideoChip msVideoFirst,const SVideoChip msVideoSecond)
- {
- return (msVideoFirst.mi64VideoCapacity < msVideoSecond.mi64VideoCapacity);
- }
- void * VideoSort(QList<SVideoChip>* msVideoChipList)
- {
- qSort(msVideoChipList->begin(),msVideoChipList->end(),CapitySort);
- // std::sort(msVideoChipList->begin(),msVideoChipList->end(),CapitySort);
- }