gpt4 book ai didi

c++ - 理解指向函数的指针的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:58:27 26 4
gpt4 key购买 nike

在 selectionsort 的签名之前,我对大部分程序都很好,其中有一个指向名为 compare 的函数的指针,但我在这段代码中的任何地方都看不到该函数。我想我想问的是比较是如何工作的?

// 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

最佳答案

where there is a pointer to a function called compare

void selectionSort( int work[], const int size,bool (*compare)( int, int ) ) compare 只是 (最后)论点。就像 work 是您作为第一个参数传入的数组(实际上是指针...)的局部名称一样,compare 是您传递的函数指针的局部名称

在此代码中,您将函数指针传递给 ascendingdescending 函数。

关于c++ - 理解指向函数的指针的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7261445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com