找回密码
 注册
X系列官方授权正版
搜索
楼主: Fred

[求助] HOW

[复制链接]
发表于 2008-12-23 19:40:59 | 显示全部楼层
  1. #include <cstdlib>
  2. #include <iostream>
  3. using namespace std;

  4. int values[] = { 40, 10, 100, 90, 20, 25 };

  5. int compare (const void * a, const void * b)
  6. {
  7.   return ( *(int*)a - *(int*)b );
  8. }

  9. int main ()
  10. {
  11.   int n;
  12.   qsort (values, 6, sizeof(int), compare);
  13.   for (n=0; n<6; n++) cout << values[n];
  14.   return 0;
  15. }
复制代码


Qsort:
void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
<cstdlib>
Sort elements of array

Sorts the num elements of the array pointed by base, each element size bytes long, using the comparator function to determine the order.

The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements of the array.

The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.

Parameters

base
    Pointer to the first element of the array to be sorted.
num
    Number of elements in the array pointed by base.
size
    Size in bytes of each element in the array.
comparator
    Function that compares two elements. The function shall follow this prototype:

    int comparator ( const void * elem1, const void * elem2 );

    The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.

    The return value of this function should represent whether elem1 is considered less than, equal, or grater than elem2 by returning, respectivelly, a negative value, zero or a positive value.

Return Value
(none)
回复

使用道具 举报

发表于 2008-12-23 19:41:51 | 显示全部楼层
有 Mathematica 就更简单了... C# 也自带排序...
STL 里也有排序...
回复

使用道具 举报

发表于 2008-12-24 02:49:46 | 显示全部楼层
{:4_102:} 倒,居然有qsort函数用来排序的,之前遇到排序我就想冒泡的。。。

不过C#中数组类自带排序方法这个我还是知道的。
回复

使用道具 举报

 楼主| 发表于 2008-12-24 21:12:58 | 显示全部楼层
两个代码都分析过了,very useful,谢谢。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Archiver|手机版|小黑屋|DeepTimes.NET 太空游戏站