|
本帖最后由 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ù)。
- #include <iostream>
- #include <vector>
- #include <algorithm> //copy算法
- #include <numeric> //accumulate算法
- #include <functional>
- #include <iterator> //輸出迭代器
- using namespace std;
-
- //定義sumSquares函數(shù),它計(jì)算第二個(gè)實(shí)參value的平方,并將結(jié)果和第一個(gè)實(shí)參相加,返回二者之和。
- 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函數(shù)將它所迭代的序列的每個(gè)元素作為第二個(gè)實(shí)參傳遞給sumSquares函數(shù)
- //第一次調(diào)用sumSquares函數(shù)時(shí),第一個(gè)實(shí)參是total的初始值(作為accumulate的第三個(gè)實(shí)參提供,在這個(gè)例子中為0)
- //在sumSquares函數(shù)的所有后續(xù)調(diào)用中,傳給它的第一個(gè)實(shí)參是前一次調(diào)用sumSquares時(shí)所返回的當(dāng)前和。
- //當(dāng)accumulate結(jié)束時(shí),它返回序列中所有元素的平方和。
- result = accumulate(integers.begin(),integers.end(),0,sumSquares);//用一個(gè)指向sumSquares的函數(shù)指針作為最后一個(gè)實(shí)參調(diào)用accumulate函數(shù)
-
- cout<<"\n\nSum of square of element in integers using "
- <<"binary\nfuncion sunSquare: "<<result;
-
- cout<<endl;
- system("pause");
- return 0;
- }
復(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í)參,并且不返回任何值。
- #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個(gè)函數(shù)指針保存在數(shù)組f中
-
- int choice;
-
- cout << "Enter a number between 0 and 2,3 to end: ";
- cin >> choice;
-
- //處理用戶的選擇
- while ((choice >= 0) && (choice <3))
- {
- //調(diào)用數(shù)組f中的一個(gè)函數(shù)
- (*f[choice])(choice); //f[choice]選擇在數(shù)組中位置為choice的指針。
- //指針被解除引用,以調(diào)用函數(shù),并且choice作為實(shí)參傳遞給這個(gè)函數(shù)。
- 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";
- }
復(fù)制代碼
運(yùn)行結(jié)果:

|
|