Be the callback function of C + + Master

Source: Internet
Author: User

The sort function in the previous section can only be ordered in a positive direction, what if we need to reverse sort? You can add a parameter, pass in true, it means to forward the row, passing false, it means to reverse the row. To change the sort direction, just change the two comparisons with a greater than or less than sign. But there is a higher level of gameplay where the incoming parameter is not a bool value, but a function pointer.

The function name itself is a pointer, the function name when debugging, its value is an address. But logically you cannot call a function pointer, you must create another pointer to the function, and the function is called by this pointer to call the callback.

function refers to the variable pointer is not the real difference, is to save a memory location of an integer, but the compiler must be able to distinguish between the type of pointers. So the function pointer must specify its type when it is defined. So you define the type for the function. The function type is determined by the function return value type, the number of parameters, the parameter type, regardless of the function's implementation. A long-like function is the same type.

Here we add a parameter, and the incoming function pointer points to a comparison function, instead of the "arr[j]>arr[j+1" section. The comparison function must have two parameters pointing to the ordinal of the element to be compared, and the return must be of type bool, indicating the result of the comparison. The IF statement is based on the return value of the comparison function to determine whether to swap locations.

#include <stdio.h>//Define comparison function typetypedef int(*comparefunc) (int*,int,int);//Pre-declaration sort function, the third parameter is the comparison function typevoidSortint* ,int, Comparefunc);//Return non 0 number indicates success, return 0 indicates failureintCompare1 (int*arr,intAintb) {returnARR[A]&GT;ARR[B];}intCompare2 (int*arr,intAintb) {returnARR[A]&LT;ARR[B];}intMainintargcConst Char* argv[]) {intarr[]={ A, -, $,1,2,7, the,554,23424234, A};//Sort, the third parameter can pass in one of two comparison functionsSort (arr,sizeof(arr)/sizeof(int), Compare1);//print sorted array     for(intI=0;i<sizeof(arr)/sizeof(int); i++) {printf("%d", Arr[i]); }return 0;}The first parameter is the array to sort, and the second is the number of elements in the array twovoidSortint*arr,intCount,comparefunc Compare_func) {intI//Outer loop is coming backwards.     for(i=count-1;i>0; i--) {intJ for(j=0; j<i;j++) {//Call comparison function to compare            if(Compare_func (Arr,j,i)) {intTMP = Arr[j];                Arr[j]=arr[i];            arr[i]=tmp; }        }    }}

What are the benefits of using a callback function instead? Here is not obvious, even more troublesome. But imagine this: if we don't have a simple value in the array, it's a structure. For example, poker, a card has at least two properties: color and points, corresponding to the structure is two member variables. When the card is sent to the player's hands, it is disorderly order, the player needs to tidy up its sequence. Different games have different sorting rules, so we can write a different sort function for each game rule, or do this: the sort function is fixed and the comparison function is written for each different game. This enhances the reusability of the sorting code.
Previous: Become a C + + master for the loop

Be the callback function of C + + Master

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.