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

函數指針與函數返回指針區別:
例如:
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,每個函數都帶一個整形實參,并且不返回任何值。
- #include <iostream>
- using namespace std;
-
- void function0(int);
- void function1(int);
- void function2(int);
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- void (*f[3])(int) = {function0,function1,function2}; //將這3個函數指針保存在數組f中
-
- int choice;
-
- cout << "Enter a number between 0 and 2,3 to end: ";
- cin >> choice;
-
- //處理用戶的選擇
- while ((choice >= 0) && (choice <3))
- {
- //調用數組f中的一個函數
- (*f[choice])(choice); //f[choice]選擇在數組中位置為choice的指針。
- //指針被解除引用,以調用函數,并且choice作為實參傳遞給這個函數。
- cout << "Enter a number between 0 and 2,3 to end: ";
- cin >> choice;
- }
-
- cout << "Program execution completed." << endl;
- system("pause");
- return 0;
- }
-
- void function0(int a)
- {
- cout << "You entered" << a << " so function0 was called\n\n";
- }
-
- void function1(int b)
- {
- cout << "You entered" << b << " so function0 was called\n\n";
- }
-
- void function2(int c)
- {
- cout << "You entered" << c << " so function0 was called\n\n";
- }
復制代碼
運行結果:

|