Function Pointers
来源:互联网 发布:妖怪名单实体娃娃 淘宝 编辑:程序博客网 时间:2024/05/29 14:44
/*$T Fig08_20.cpp GC 1.140 04/10/12 15:37:25 *//* 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 *//* ======================================================================================================================= 2012.04.10 By KeKe ======================================================================================================================= */void 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 originalorder\n"; /* output original array */ for(counter = 0; counter < arraySize; ++counter) cout << setw(4) << a[counter]; /* * sort array in ascending order; * passfunction 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 (orlargest) 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 a scending order sort ======================================================================================================================= */bool ascending(int a, int b){ return a < b; /* returns true ifa 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 function descending */
O(∩_∩)O
- Function Pointers
- Function Pointers
- Function pointers
- Pointers to Function, ++
- Abstraction and function pointers
- Pointers to Function
- 函数指针(Function Pointers)
- Programs as Data: Function Pointers
- Example Uses of Function Pointers
- Function Pointers, Functors, Virtual Functions
- Item 14. 函数指针(Function Pointers)
- CallBack function from Pointers on C
- Programs as Data: Function Pointers(函数指针)
- Function Pointers and Callbacks in C
- Function Pointers<外文转载>http://www.learncpp.com/cpp-tutorial/78-function-pointers/
- Member Function Pointers and the Fastest Possible C++ Delegates
- [ZT]Member Function Pointers and the Fastest Possible C++ Delegates
- Member Function Pointers and the Fastest Possible C++ Delegates
- 【编程好习惯】 引入中间变量使程序更易读
- 软件测试工程师面试杂谈
- android 导入项目报错
- 详解C#装箱与拆箱
- linux重定向命令应用及语法
- Function Pointers
- 了解node.js
- 全国各地计算机技术与软件资格考试机构网站汇总
- 在服务器端写查询LINQ的过滤条件
- Lilliputian燃料电池块头小电量足 可为智能手机充电十多次
- 两个bean之间的复制
- vc++字符转换问题
- 预编译理解
- 【转载】不管你挣多少,钱永远是问题!