C++ examples for Function:Function Pointer
Multipurpose sorting program using function pointers.
#include <iostream> #include <iomanip> using namespace std; // prototypes /*from w w w . j av a 2 s. com*/ 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 ] = { 122, 612, 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; for ( counter = 0; counter < arraySize; ++counter ) cout << setw( 4 ) << a[ counter ]; if ( order == 1 ) { selectionSort( a, arraySize, ascending ); cout << "\nData items in ascending order\n"; }else { selectionSort( a, arraySize, descending ); cout << "\nData items in descending order\n"; } for ( counter = 0; counter < arraySize; ++counter ) cout << setw( 4 ) << a[ counter ]; cout << endl; } 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 ] ); } } void swap( int * const element1Ptr, int * const element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; } bool ascending( int a, int b ) { return a < b; // returns true if a is less than b } bool descending( int a, int b ) { return a > b; // returns true if a is greater than b }