为了让对方决定如何排序来完成,而不是用我们自己的硬编码的比较函数,我们将允许调用者提供自己的排序功能!这是通过一个函数指针。
由于呼叫者的比较函数来比较两个整数并返回一个布尔值,一个指向这样的功能可能会是这样子的:
1
布尔(* pcomparison)(int,int);
因此,我们将允许调用者通过我们的排序例程的指针到期望的比较函数的第三个参数,然后我们再利用对方的功能做的比较。
这是一个选择排序,使用函数指针参数做一个用户自定义的比较完整的例子,以及一个示例如何调用它:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#include
<algorithm> // for swap //
Note our user-defined comparison is the third parameter void
SelectionSort( int
*anArray, int
nSize, bool
(*pComparison)( int ,
int )) { using
namespace
std; for
( int
nStartIndex= 0; nStartIndex < nSize; nStartIndex++) { int
nBestIndex = nStartIndex; //
Search through every element starting at nStartIndex+1 for
( int
nCurrentIndex = nStartIndex + 1; nCurrentIndex < nSize; nCurrentIndex++) { //
Note that we are using the user-defined comparison here if
(pComparison(anArray[nCurrentIndex], anArray[nBestIndex])) //
COMPARISON DONE HERE nBestIndex
= nCurrentIndex; } //
Swap our start element with our best element swap(anArray[nStartIndex],
anArray[nBestIndex]); } } //
Here is a comparison function that sorts in ascending order //
(Note: it's exactly the same as the previous Ascending() function) bool
Ascending( int
nX, int
nY) { return
nY > nX; } //
Here is a comparison function that sorts in descending order bool
Descending( int
nX, int
nY) { return
nY < nX; } //
This function prints out the values in the array void
PrintArray( int
*pArray, int
nSize) { for
( int
iii=0; iii < nSize; iii++) cout
<< pArray[iii] << "
" ; cout
<< endl; } int
main() { using
namespace
std; int
anArray[9] = { 3, 7, 9, 5, 6, 1, 8, 2, 4 }; //
Sort the array in descending order using the Descending() function SelectionSort(anArray,
9, Descending); PrintArray(anArray,
9); //
Sort the array in ascending order using the Ascending() function SelectionSort(anArray,
9, Ascending); PrintArray(anArray,
9); return
0; } |
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#include
<algorithm> // for swap //
Note our user-defined comparison is the third parameter void
SelectionSort( int
*anArray, int
nSize, bool
(*pComparison)( int ,
int )) { using
namespace
std; for
( int
nStartIndex= 0; nStartIndex < nSize; nStartIndex++) { int
nBestIndex = nStartIndex; //
Search through every element starting at nStartIndex+1 for
( int
nCurrentIndex = nStartIndex + 1; nCurrentIndex < nSize; nCurrentIndex++) { //
Note that we are using the user-defined comparison here if
(pComparison(anArray[nCurrentIndex], anArray[nBestIndex])) //
COMPARISON DONE HERE nBestIndex
= nCurrentIndex; } //
Swap our start element with our best element swap(anArray[nStartIndex],
anArray[nBestIndex]); } } //
Here is a comparison function that sorts in ascending order //
(Note: it's exactly the same as the previous Ascending() function) bool
Ascending( int
nX, int
nY) { return
nY > nX; } //
Here is a comparison function that sorts in descending order bool
Descending( int
nX, int
nY) { return
nY < nX; } //
This function prints out the values in the array void
PrintArray( int
*pArray, int
nSize) { for
( int
iii=0; iii < nSize; iii++) cout
<< pArray[iii] << "
" ; cout
<< endl; } int
main() { using
namespace
std; int
anArray[9] = { 3, 7, 9, 5, 6, 1, 8, 2, 4 }; //
Sort the array in descending order using the Descending() function SelectionSort(anArray,
9, Descending); PrintArray(anArray,
9); //
Sort the array in ascending order using the Ascending() function SelectionSort(anArray,
9, Ascending); PrintArray(anArray,
9); return
0; } |