找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4717|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

函數(shù)指針與函數(shù)指針數(shù)組的使用方法

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:77367 發(fā)表于 2015-4-19 02:18 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 bibi 于 2015-4-19 02:19 編輯

函數(shù)指針:

函數(shù)指針包含函數(shù)在內(nèi)存中的地址。數(shù)組名實(shí)際上就是數(shù)組的第一個(gè)元素在內(nèi)存中的地址,類似地,函數(shù)名實(shí)際上也是執(zhí)行這個(gè)函數(shù)任務(wù)的代碼在內(nèi)存中的起始地址。
函數(shù)指針可以傳遞給函數(shù)、從函數(shù)返回、保存在數(shù)組中、賦予另一個(gè)函數(shù)指針或者調(diào)用底層函數(shù)。
下面我們用數(shù)值算法accumulate討論下函數(shù)指針的用法。accumulate是一種常用的STL數(shù)學(xué)算法。
std::accumulate(v.begin(),v.end(),0);是對v中從v.begin()開始,直到v.end()(但不包括這個(gè)位置)范圍內(nèi)的元素求和。
這個(gè)函數(shù)的第二個(gè)版本的第四個(gè)實(shí)參是一個(gè)通用函數(shù),它確定了如何對元素求和。這個(gè)通用函數(shù)必須帶兩個(gè)實(shí)參并返回一個(gè)結(jié)果。第一個(gè)實(shí)參是和的當(dāng)前值,第二個(gè)實(shí)參是序列中被求和的當(dāng)前元素的值。
許多STL算法允許將函數(shù)指針傳遞到算法中,以幫助算法執(zhí)行任務(wù)。
下面demo使用函數(shù)指針演示了accumulate函數(shù)。
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <algorithm>         //copy算法  
  4. #include <numeric>          //accumulate算法  
  5. #include <functional>  
  6. #include <iterator>            //輸出迭代器  
  7. using namespace std;  
  8.   
  9. //定義sumSquares函數(shù),它計(jì)算第二個(gè)實(shí)參value的平方,并將結(jié)果和第一個(gè)實(shí)參相加,返回二者之和。  
  10. int sumSquares(int total,int value)  
  11. {  
  12. return total + value*value;  
  13. }  
  14. int _tmain(int argc, _TCHAR* argv[])  
  15. {  
  16. const int SIZE = 10;  
  17. int array[SIZE] = {1,2,3,4,5,6,7,8,9,10};  
  18. vector<int> integers(array,array+SIZE);     //元素拷貝  
  19. ostream_iterator<int> output(cout," ");  
  20. int result;  
  21. cout<<"vector integers contains:\n";  
  22. copy(integers.begin(),integers.end(),output);  
  23.   
  24. //accumulate函數(shù)將它所迭代的序列的每個(gè)元素作為第二個(gè)實(shí)參傳遞給sumSquares函數(shù)  
  25. //第一次調(diào)用sumSquares函數(shù)時(shí),第一個(gè)實(shí)參是total的初始值(作為accumulate的第三個(gè)實(shí)參提供,在這個(gè)例子中為0)  
  26. //在sumSquares函數(shù)的所有后續(xù)調(diào)用中,傳給它的第一個(gè)實(shí)參是前一次調(diào)用sumSquares時(shí)所返回的當(dāng)前和。  
  27. //當(dāng)accumulate結(jié)束時(shí),它返回序列中所有元素的平方和。  
  28. result = accumulate(integers.begin(),integers.end(),0,sumSquares);//用一個(gè)指向sumSquares的函數(shù)指針作為最后一個(gè)實(shí)參調(diào)用accumulate函數(shù)  
  29.   
  30. cout<<"\n\nSum of square of element in integers using "  
  31.   <<"binary\nfuncion sunSquare: "<<result;  
  32.   
  33. cout<<endl;  
  34. system("pause");  
  35. return 0;  
  36. }  
復(fù)制代碼


運(yùn)行結(jié)果:




函數(shù)指針與函數(shù)返回指針區(qū)別:
例如:
Void selectionSort(int work[],const int size,bool(*compare)(int,int))
在上面selectionSort的函數(shù)中出現(xiàn)了參數(shù)bool(*compare)(int,int)
這個(gè)參數(shù)指定一個(gè)函數(shù)指針。關(guān)鍵之bool表明被指向的函數(shù)返回一個(gè)bool值。
文本(*compare)表示這個(gè)函數(shù)指針的名稱(*表明參數(shù)compare是一個(gè)指針)。
文本“(int,int)”表示compare指向的函數(shù)接受兩個(gè)整形實(shí)參。
“*compare”兩邊的圓括號是必須的,它表示compare是一個(gè)函數(shù)指針。
如果沒有圓括號,則聲明變成bool *compare(int,int)
它聲明了一個(gè)函數(shù),這個(gè)函數(shù)接收兩個(gè)整數(shù)作為參數(shù),并返回一個(gè)指向bool值的指針。

函數(shù)指針數(shù)組
函數(shù)指針的一個(gè)用法出現(xiàn)在菜單驅(qū)動(dòng)系統(tǒng)中。例如程序可以提示用戶輸入一個(gè)整數(shù)值來選擇菜單中的一個(gè)選項(xiàng)。用戶的選擇可以做函數(shù)指針數(shù)組的下標(biāo),而數(shù)組中的指針可以用來調(diào)用函數(shù)。
下面的demo提供了一個(gè)機(jī)械的例子,它演示了函數(shù)指針數(shù)組的聲明和使用。在程序中定義了3個(gè)函數(shù):function0, function1和function2,每個(gè)函數(shù)都帶一個(gè)整形實(shí)參,并且不返回任何值。

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. void function0(int);  
  5. void function1(int);  
  6. void function2(int);  
  7.   
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {  
  10. void (*f[3])(int) = {function0,function1,function2};  //將這3個(gè)函數(shù)指針保存在數(shù)組f中  
  11.   
  12. int choice;  
  13.   
  14. cout << "Enter a number between 0 and 2,3 to end: ";  
  15. cin >> choice;  
  16.   
  17. //處理用戶的選擇  
  18. while ((choice >= 0) && (choice <3))  
  19. {  
  20.   //調(diào)用數(shù)組f中的一個(gè)函數(shù)  
  21.   (*f[choice])(choice);   //f[choice]選擇在數(shù)組中位置為choice的指針。  
  22.                          //指針被解除引用,以調(diào)用函數(shù),并且choice作為實(shí)參傳遞給這個(gè)函數(shù)。  
  23.   cout << "Enter a number between 0 and 2,3 to end: ";  
  24.   cin >> choice;  
  25. }  
  26.   
  27. cout << "Program execution completed." << endl;  
  28. system("pause");  
  29. return 0;  
  30. }  
  31.   
  32. void function0(int a)  
  33. {  
  34. cout << "You entered" << a << " so function0 was called\n\n";  
  35. }  
  36.   
  37. void function1(int b)  
  38. {  
  39. cout << "You entered" << b << " so function0 was called\n\n";  
  40. }  
  41.   
  42. void function2(int c)  
  43. {  
  44. cout << "You entered" << c << " so function0 was called\n\n";  
  45. }  
復(fù)制代碼



  運(yùn)行結(jié)果:




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表