函数指针和回调在C/C++中

huangapple go评论50阅读模式
英文:

Function pointers and callbacks in c/c++

问题

我试图回忆 C 和 C++ 中指针的工作原理,发现了这个非常有趣的视频(C/C++ 中的指针)。在视频的 3:14:00 处,他开始讨论指针函数和回调,我对它们的实际应用感到有些困惑。

他提供的示例案例包括一个排序算法,它将一个函数指针作为参数,该函数指针定义了要遵循的比较 "规则"(从大到小的顺序,从小到大的顺序,根据绝对值给出的从大到小的顺序...)。最终,他以类似这样的内容结束:

#include <stdio.h>
int compare(int a, int b){
    if(a > b) return -1;
    return 1;
}
void MyBubbleSort(int A[], int n, int (*compare)(int,int)){
    int i,j,temp;
    for(i=0; i<n; i++){
        for(j=0; j<n-1; j++){
            if(compare(A[j], A[j+1]) > 0{
                temp = A[j];
                A[j] = A[j+1];
                A[j+1] = temp;
            }
        }
    }
}
int main(){
    int i, A[] = {3,2,1,5,6,4};
    MyBubbleSort(A,6,compare);
    for(i=0; i<6; i++) printf("%d ",A[i]);
}

当他想要更改比较规则时,他只需更改 compare(int, int) 的内容,就可以了。我的问题是,为什么他要这样做,而不是只有一个名为 compare(int, int) 的单独函数,它只是做与代码片段中所示的相同的事情,并且只从 MyBubbleSort(int[], int) 中调用该函数。它不会有相同的行为吗?那么有什么好处呢?还有其他有趣的用例吗?

非常感谢回答!

英文:

I was trying to remind how pointers worked in c and c++ and I found this very interesting video (Pointers in C/C++). In minute 3:14:00, he begins to talk about pointer functions and callbacks, and I ended a bit confused about the real application of them.

The example case he provides consists of a sorting algorithm that takes a function pointer as argument, which defines the comparison "rule" to follow (order from geater to smaller, order from smaller to greater, from greater to smaller given the absolute values...). He eventually ends with something like this:

#include&lt;stdio.h&gt;
int compare(int a, int b){
    if(a &gt; b) return -1;
    return 1;
}
void MyBubbleSort(int A[], int n, int (*compare)(int,int)){
    int i,j,temp;
    for(i=0; i&lt;n; i++){
        for(j=0; j&lt;n-1; j++){
            if(compare(A[j], A[j+1]) &gt; 0{
                temp = A[j];
                A[j] = A[j+1];
                A[j+1] = temp;
            }
        }
    }
}
int main(){
    int i, A[] = {3,2,1,5,6,4};
    MyBubbleSort(A,6,compare);
    for(i=0; i&lt;6; i++) printf(&quot;%d &quot;,A[i]);
}

When he wants to change the comparison rule, he changes the compare(int, int) content and that's all. My question is, why would he do that, instead of just having a separate function called compare(int, int) that just does the same as the one showed in the code snippet, and just call that function from within MyBubbleSort(int[], int). Wouldn't it just have the same behaviour? What are the benefits then? Are there any other interesting use cases?

Thank you very much for answers!

答案1

得分: 2

如果我正确理解您的问题,主要观点是在叙述中提到的,但没有包含在示例代码中:您可以在程序中拥有几个不同的比较函数,并在适当的时候使用它们,同时仍然只有一个MyBubbleSort函数。

一个示例可能如下所示:

int compare_increasing(int a, int b){
    if(a > b) return -1;
    return 1;
}

int compare_decreasing(int a, int b){
    if(a < b) return -1;
    return 1;
}

void MyBubbleSort(int A[], int n, int (*compare)(int,int));

int main(){
    // ...

    // 按升序排序
    MyBubbleSort(A,6,compare_increasing);

    // 按降序排序
    MyBubbleSort(A,6,compare_decreasing);
}
英文:

If I understand your question correctly, the main point is something that he mentions in the narration but does not include in the sample code: you could have several different comparison functions in your program, and use them at different times as appropriate, while still having just one MyBubbleSort function.

An example might look like:

int compare_increasing(int a, int b){
    if(a &gt; b) return -1;
    return 1;
}

int compare_decreasing(int a, int b){
    if(a &lt; b) return -1;
    return 1;
}

void MyBubbleSort(int A[], int n, int (*compare)(int,int));


}
int main(){
    // ...

    // sort in increasing order
    MyBubbleSort(A,6,compare_increasing);

    // sort in decreasing order
    MyBubbleSort(A,6,compare_decreasing);
}

答案2

得分: 1

  1. Calling a function by a pointer in this case makes the sorting function universal.
  2. If you hardcode the comparison in that function it will work only for that condition.
  3. C does not have lambda expressions and anonymous functions so the compare function has to be written separately and the pointer passed.
英文:
  1. Calling a function by a pointer in this case makes the sorting function universal.
  2. If you hardcode the comparison in that function it will work only for that condition.
  3. C does not have lambda expressions and anonymous functions so the compare function has to be written separately and the pointer passed.

huangapple
  • 本文由 发表于 2023年2月19日 01:13:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定