简体   繁体   English

编程珍珠中的c标准库排序功能

[英]c standard library sort function from programming pearls

#include <algorithm>
#include <stdio.h>
#include <iostream>



int intcomp(int *x,int *y) {  return *x-*y;};
int a[10000];
int main(void){
    int i; int n=0;
     while (scanf("%d",&a[n])!=EOF)
          n++;
     qsort(a,n,sizeof(int),intcomp);
      for (int i=0;i<n;i++)
           printf("%d\n",a[i]);
       return 0;


}

it is just copy of code i have two question it show me that intcomp is incompatible in this code and also what does intcomp function? 它只是代码的副本,我有两个问题,向我表明intcomp在此代码中不兼容,而且intcomp的功能是什么? and also what is in windows 7 EOF? Windows 7 EOF中也有什么? how tell program that it reached EOF? 如何告诉程序已达到EOF?

the qsort() function requires a pointer with a particular signature. qsort()函数需要带有特定签名的指针。
Your function has the wrong signature so it is complaining. 您的函数签名错误,因此在抱怨。

Your function has the signature: 您的函数具有签名:

int intcomp(int *x,int *y)

While qsort requires the signature: 虽然qsort需要签名:

int intcomp(void const* xp,void const* yp)

Please note the difference in the parameter types. 请注意参数类型的差异。

A corrected version of the function is: 该函数的更正版本是:

int intcomp(void const* xp,void const* yp)
{
  // Modified for C as the tag on the question changed:
  // int x = *static_cast<int const*>(xp);
  // int y = *static_cast<int const*>(yp);
  int x = *((int const*)(xp));
  int y = *((int const*)(yp));

  return x-y;
}

The function qsort() is passed a function pointer as the third parameter. 向函数qsort()传递了一个函数指针作为第三个参数。
This function pointer (in your case intcomp()) is used to compare values in the array passed. 此函数指针(在您的情况下为intcomp())用于比较传递的数组中的值。 Each call provides pointers into the array. 每个调用都提供指向数组的指针。 The result of the function should be: 该函数的结果应为:

Less than 0:    if x is smaller than y
0:              If x is equal to y
Greater than 0: If x is larger than y

intcomp is an "Int Compare" function. intcomp是一个“内部比较”功能。 It is passed a pointer to 2 ints and returns 0 if they are the same, a positive value is x > y and a negative value is x < y. 向其传递一个指向2个int的指针,如果它们相同,则返回0,正值是x> y,负值是x <y。

qsort is passed a pointer to this function and calls it each time it wants to know how to sort a pair of values. qsort传递了一个指向该函数的指针,并在每次它想知道如何对一对值进行排序时调用它。

The docs for qsort should give you some more details. qsort的文档应该为您提供更多详细信息。
eg http://www.cppreference.com/wiki/c/other/qsort 例如http://www.cppreference.com/wiki/c/other/qsort

qsort is in stdlib.h , so include that file at the beginning. qsortstdlib.h ,因此请在开头包含该文件。 Note that algorithm and iostream aren't needed. 请注意,不需要algorithmiostream

#include <stdlib.h>

First of all: the question is labeled C++ and you #include <algorithm> and <iostream>, but your code is 100% C. 首先:问题被标记为C ++,并且您#include <algorithm>和<iostream>,但是您的代码是100%C。

Martin York already gave the answer how to correct the signature of the function you pass to qsort(). Martin York已经给出了如何更正传递给qsort()的函数签名的答案。

However, the "true"(TM) C++ solution would be to use std::sort<> instead of qsort! 但是,“ true”(TM)C ++解决方案是使用std :: sort <>而不是qsort!

#include <algorithm>    
#include <stdio.h>    

bool intcomp(int a, int b) { return a<b; }
int a[10000];    
int main(void){    
    int n=0;    
     while (scanf("%d",&a[n])!=EOF)    
          n++;    
     std::sort(&a[0], &a[n], intcomp);
      for (int i=0;i<n;i++)    
           printf("%d\n",a[i]);    
       return 0;    
}

Note that incomp() takes ints and not int pointers, and returns a bool. 请注意,incomp()采用int而非int指针,并返回布尔值。 Just like operator<() would. 就像operator <()一样。

Also note that in this case, you could forget the intcomp and just use std::sort(&a[0], &a[n]), which will use std::less<>, which will use operator<(int, int). 另请注意,在这种情况下,您可能会忘记intcomp而仅使用std :: sort(&a [0],&a [n]),它将使用std :: less <>,而将使用operator <(int,int )。

As Martin York mentioned, qsort needs a function which it will use to compare the values: 正如马丁·约克(Martin York)所述,qsort需要一个函数来比较值:

void qsort( void *buf, size_t num, size_t size, int (*compare)(const void*, const void *) );

Here is a good example on how to use qsort: http://www.cppreference.com/wiki/c/other/qsort 这是有关如何使用qsort的一个很好的示例: http ://www.cppreference.com/wiki/c/other/qsort

Edit: Ri was faster.... 编辑:Ri更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM