1
| int Array [5]; |
*Array
or Array[Index]
), the appropriate array element is returned.
1
| int foo(); |
1
2
| // pFoo is a pointer to a function that takes no arguments and returns an integer int (*pFoo) (); |
int *pFoo()
would be interpreted as a function named pFoo that takes no parameters and returns a pointer to an integer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| int foo() { } int goo() { } int main() { int (*pFoo)() = foo; // pFoo points to function foo() pFoo = goo; // pFoo now points to function goo() return 0; } |
1
| pFoo = goo(); |
1
2
3
4
5
6
7
8
9
10
11
| // function prototypes int foo(); double goo(); int hoo( int nX); // function pointer assignments int (*pFcn1)() = foo; // okay int (*pFcn2)() = goo; // wrong -- return types don't match! double (*pFcn3)() = goo; // okay pFcn1 = hoo; // wrong -- pFcn1 has no parameters, but hoo() does int (*pFcn3)( int ) = hoo; // okay |
1
2
3
4
5
6
7
| int foo( int nX) { } int (*pFoo)( int ) = foo; // assign pFoo to foo() (*pFoo)(nValue); // call function foo(nValue) through pFoo. |
1
2
3
4
5
6
7
| int foo( int nX) { } int (*pFoo)( int ) = foo; // assign pFoo to foo() pFoo(nValue); // call function foo(nValue) through pFoo. |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| void SelectionSort( int *anArray, int nSize) { 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 (anArray[nCurrentIndex] < anArray[nBestIndex]) // COMPARISON DONE HERE nBestIndex = nCurrentIndex; } // Swap our start element with our best element swap(anArray[nStartIndex], anArray[nBestIndex]); } } |
1
2
3
4
| bool Ascending( int nX, int nY) { return nY > nX; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| void SelectionSort( int *anArray, int nSize) { 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 (Ascending(anArray[nCurrentIndex], anArray[nBestIndex])) // COMPARISON DONE HERE nBestIndex = nCurrentIndex; } // Swap our start element with our best element swap(anArray[nStartIndex], anArray[nBestIndex]); } } |
1
| bool (*pComparison)( int , int ); |
1
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; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| bool EvensFirst( int nX, int nY) { // if nX is not even and nY is, nY goes first if ((nX % 2) && !(nY % 2)) return false ; // if nX is even and nY is not, nX goes first if (!(nX % 2) && (nY % 2)) return true ; // otherwise sort in Ascending order return Ascending(nX, nY); } int main() { using namespace std; int anArray[9] = { 3, 7, 9, 5, 6, 1, 8, 2, 4 }; SelectionSort(anArray, 9, EvensFirst); PrintArray(anArray, 9); return 0; } |
1
| typedef bool (*pfcnValidate)( int , int ); |
1
| bool Validate( int nX, int nY, bool (*pfcn)( int , int )); |
1
| bool Validate( int nX, int nY, pfcnValidate pfcn) |
0 comments: