Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem understanding a pointer to a function

Tags:

c++

I'm fine with most of the program up until the signature of selectionsort where there is a pointer to a function called compare but I don't see the function anywhere in this code. I guess what I'm trying to ask is how is compare working?

// Fig. 8.20: fig08_20.cpp
// Multipurpose sorting program using function pointers.
#include <iostream>
#include <iomanip>
using namespace std;

// prototypes
void selectionSort( int [], const int, bool (*)( int, int ) );
void swap( int * const, int * const );   
bool ascending( int, int ); // implements ascending order
bool descending( int, int ); // implements descending order

int main()
{
   const int arraySize = 10;
   int order; // 1 = ascending, 2 = descending
   int counter; // array index
   int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

   cout << "Enter 1 to sort in ascending order,\n" 
      << "Enter 2 to sort in descending order: ";
   cin >> order;
   cout << "\nData items in original order\n";

   // output original array
   for ( counter = 0; counter < arraySize; counter++ )
      cout << setw( 4 ) << a[ counter ];

   // sort array in ascending order; pass function ascending 
   // as an argument to specify ascending sorting order
   if ( order == 1 ) 
   {
      selectionSort( a, arraySize, ascending );
      cout << "\nData items in ascending order\n";
   } // end if

   // sort array in descending order; pass function descending
   // as an argument to specify descending sorting order
   else 
   {
      selectionSort( a, arraySize, descending );
      cout << "\nData items in descending order\n";
   } // end else part of if...else

   // output sorted array
   for ( counter = 0; counter < arraySize; counter++ )
      cout << setw( 4 ) << a[ counter ];

   cout << endl;
} // end main

// multipurpose selection sort; the parameter compare is a pointer to
// the comparison function that determines the sorting order
void selectionSort( int work[], const int size,
                    bool (*compare)( int, int ) )
{
   int smallestOrLargest; // index of smallest (or largest) element

   // loop over size - 1 elements
   for ( int i = 0; i < size - 1; i++ )
   {
      smallestOrLargest = i; // first index of remaining vector

      // loop to find index of smallest (or largest) element
      for ( int index = i + 1; index < size; index++ )
         if ( !(*compare)( work[ smallestOrLargest ], work[ index ] ) )
            smallestOrLargest = index;

      swap( &work[ smallestOrLargest ], &work[ i ] );
   } // end if
} // end function selectionSort

// swap values at memory locations to which 
// element1Ptr and element2Ptr point
void swap( int * const element1Ptr, int * const element2Ptr )
{
   int hold = *element1Ptr;
   *element1Ptr = *element2Ptr;
   *element2Ptr = hold;
} // end function swap

// determine whether element a is less than 
// element b for an ascending order sort
bool ascending( int a, int b )
{
   return a < b; // returns true if a is less than b
} // end function ascending

// determine whether element a is greater than 
// element b for a descending order sort
bool descending( int a, int b )
{
   return a > b; // returns true if a is greater than b
} // end f   return a > b; // returns true if a is greater than b
like image 544
Lightyear Buzz Avatar asked Aug 31 '11 18:08

Lightyear Buzz


2 Answers

where there is a pointer to a function called compare

Invoid selectionSort( int work[], const int size,bool (*compare)( int, int ) ) compare is just the local name of the (last) argument. Just like work is the local name for the array(pointer actually...) you pass in as the first argument, compare is the local name of a function pointer you pass it.

In this code, you pass it function pointers to the ascending and descending functions.

like image 105
Lyke Avatar answered Sep 30 '22 15:09

Lyke


Compare is the name of the argument (variable name). It points to a function which is passed to the sort function. You can pass either ascending or descending "comparator" function as this argument value and it will determine the direction of the sort.

like image 28
MK. Avatar answered Sep 30 '22 17:09

MK.