如何傳入乙個函式指標?

時間 2021-06-05 11:50:19

1樓:劉項

雖然用C語言中的函式指標也可以達到這個效果,但是我還是比較推薦C++中的仿函式來實現。

仿函式相比函式指標有很多優點,比如型別安全。

STL中有很多地方使用了仿函式,比如sort函式。

sort(a, a+n, less());

其中第3個引數就是個仿函式,

在STL的原始碼中可以找到它的定義,

也可以參見管清文的回答。

2樓:管清文

也可以用Functor來完成

template

struct List

T operator (size_t i)struct Compare ;

cout << LocateElem(a, -1, Compare()) << endl;

cout << LocateElem(a, -2, Compare()) << endl;}

3樓:Xi Yang

你需要學習函式指標的正確寫法。

假定是int內容:

typedef bool (*CompareFuncType)(int a, int b);

LocateElem(List* l, int expected, CompareFuncType func);

在不同的gcc版本中,函式傳入指標,在函式執行結束後列印指標指向值會有不同嗎?

SPeak int plusOne int digits int digitsSize int returnSize c int plusOne int digits int digitsSize int returnSize cpp int plusOne int digits int digit...

二級指標傳入函式引數,為何執行結果為空呢?在不用陣列指標情況下,如何改變b陣列,並在主函式輸出呢?

魔某人 如果乙個東西是多維陣列,那麼像這樣的寫法 inta 10 9 a m n charb 11 45 14 b r s t 其等效寫法是 a 9 m n b 45 14 r 14 s t 而如果乙個東西是多級指標,那麼像這樣的寫法 int a a m n char b b r s t 其等效寫法...

C 中如何定義指向函式指標的指標?

zr scat 瀉藥先定義乙個函式指標 typedef void pfn void 再定義指向函式指標的指標 void print void pfn func print pfn ptr func 在c 11下,可以這麼偷懶獲得型別 1 include 23using namespace std 4...