轉自:http://blog.csdn.net/feitianxuxue/article/details/7300291
函數指針與函數指針數組的使用方法
函數指針:
函數指針包含函數在內存中的地址。數組名實際上就是數組的第一個元素在內存中的地址,類似地,函數名實際上也是執行這個函數任務的代碼在內存中的起始地址。
函數指針可以傳遞給函數、從函數返回、保存在數組中、賦予另一個函數指針或者調用底層函數。
下面我們用數值算法accumulate討論下函數指針的用法。accumulate是一種常用的STL數學算法。
std::accumulate(v.begin(),v.end(),0);是對v中從v.begin()開始,直到v.end()(但不包括這個位置)范圍內的元素求和。
這個函數的第二個版本的第四個實參是一個通用函數,它確定了如何對元素求和。這個通用函數必須帶兩個實參並返回一個結果。第一個實參是和的當前值,第二個實參是序列中被求和的當前元素的值。
許多STL算法允許將函數指針傳遞到算法中,以幫助算法執行任務。
下面demo使用函數指針演示了accumulate函數。
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函數,它計算第二個實參value的平方,並將結果和第一個實參相加,返回二者之和。 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函數將它所迭代的序列的每個元素作為第二個實參傳遞給sumSquares函數 25 //第一次調用sumSquares函數時,第一個實參是total的初始值(作為accumulate的第三個實參提供,在這個例子中為0) 26 //在sumSquares函數的所有后續調用中,傳給它的第一個實參是前一次調用sumSquares時所返回的當前和。 27 //當accumulate結束時,它返回序列中所有元素的平方和。 28 result = accumulate(integers.begin(),integers.end(),0,sumSquares);//用一個指向sumSquares的函數指針作為最后一個實參調用accumulate函數 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 }
運行結果:
函數指針與函數返回指針區別:
例如:
Void selectionSort(int work[],const int size,bool(*compare)(int,int))
在上面selectionSort的函數中出現了參數bool(*compare)(int,int)
這個參數指定一個函數指針。關鍵之bool表明被指向的函數返回一個bool值。
文本(*compare)表示這個函數指針的名稱(*表明參數compare是一個指針)。
文本“(int,int)”表示compare指向的函數接受兩個整形實參。
“*compare”兩邊的圓括號是必須的,它表示compare是一個函數指針。
如果沒有圓括號,則聲明變成bool *compare(int,int)
它聲明了一個函數,這個函數接收兩個整數作為參數,並返回一個指向bool值的指針。
函數指針數組
函數指針的一個用法出現在菜單驅動系統中。例如程序可以提示用戶輸入一個整數值來選擇菜單中的一個選項。用戶的選擇可以做函數指針數組的下標,而數組中的指針可以用來調用函數。
下面的demo提供了一個機械的例子,它演示了函數指針數組的聲明和使用。在程序中定義了3個函數:function0, function1和function2,每個函數都帶一個整形實參,並且不返回任何值。
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個函數指針保存在數組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 //調用數組f中的一個函數 21 (*f[choice])(choice); //f[choice]選擇在數組中位置為choice的指針。 22 //指針被解除引用,以調用函數,並且choice作為實參傳遞給這個函數。 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 }
運行結果: