函数指针与函数指针数组的使用方法

转自: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:
";
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<<"

Sum of square of element in integers using "
31   <<"binary
funcion 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

";
35 }
36 
37 void function1(int b)
38 {
39  cout << "You entered" << b << " so function0 was called

";
40 }
41 
42 void function2(int c)
43 {
44  cout << "You entered" << c << " so function0 was called

";
45 }

运行结果:

函数指针与函数指针数组的使用方法