函数与对象: 没啥内容 直接上示例吧
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
void display(const string sa[], int n);
int main()
{
string list[SIZE];
cout << "Enter your " << SIZE << " favorite astronomical sights:\n";
for (int i = 0; i < SIZE; i++)
{
cout << i + 1 << ": ";
getline(cin, list[i]);
}
cout << "Your list:\n";
display(list, SIZE);
return 0;
}
void display(const string sa[], int n)
{
for (int i =0; i < n; i++)
cout << i + 1 << ": " << sa[i] << endl;
}
函数与array
#include <iostream>
#include <string>
#include <array>
const int Seasons = 4;
const std::array<std::string, Seasons> Snames =
{"Spring", "Summer", "Fall", "Winter"};
void fill(std::array<double, Seasons>* pa);
void show(std::array<double, Seasons> da);
int main()
{
std::array<double, Seasons> expenses;
fill(&expenses);
show(expenses);
return 0;
}
void fill(std::array<double, Seasons>* pa)
{
using namespace std;
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> (*pa)[i];
}
}
void show(std::array<double, Seasons> da)
{
using namespace std;
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << da[i] << endl;
total += da[i];
}
cout << "Total Expenses: $" << total << endl;
}
留意 传入的是地址还是array的值
递归:
简单递归调用:
#include <iostream>
void countdown(int n);
int main()
{
countdown(4);
return 0;
}
void countdown(int n)
{
using namespace std;
cout << "Counting down ... " << n << " (n at " << &n << ")" << endl;
if ( n > 0)
countdown(n-1);
cout << n << ": Kaboom!" << " ( n at " << &n << ")" << endl;
}
复杂一丢丢的递归调用:
二分法画标尺
#include <iostream>
const int Len = 66;
const int Divs = 6;
void subdivide(char ar[], int low, int high, int level);
int main()
{
char ruler[Len];
int i;
for (i = 1; i < Len - 2; i++)
ruler[i] = ' ';
ruler[Len - 1] = '\0';
int max = Len - 2;
int min = 0;
ruler[min] = ruler[max] = '|';
std::cout << ruler << std::endl;
for (i = 1; i <= Divs; i++)
{
subdivide(ruler,min,max, i);
std::cout << ruler << std::endl;
for (int j = 1; j < Len - 2; j++)
ruler[j] = ' ';
}
return 0;
}
void subdivide(char ar[], int low, int high, int level)
{
if (level == 0)
return;
int mid = (high + low) / 2;
ar[mid] = '|';
subdivide(ar, low, mid, level - 1);
subdivide(ar, mid, high, level -1);
}
输出结果
函数指针:
double pam (int)
函数名:地址
函数名():函数的返回值
double *pf(int) 表示的是一个返回double类型指针的函数
double(*pf)(int) 表示 函数的指针
将函数赋值给函数指针 返回类型一定要与指针类型相同
pf = pam
通过函数指针来调用函数:
double y = (*pf)(5) 或者 pf(5)
示例:
#include <iostream>
double betsy(int);
double pam(int);
void estimate(int lines, double (*pf)(int));
int main()
{
using namespace std;
int code;
cout << "How many lines of code do you need? ";
cin >> code;
cout << "Here's Betsy's estimate:\n";
estimate(code , betsy);
cout << "Here's Pam's estimate:\n";
estimate(code, pam);
return 0;
}
double betsy(int lns)
{
return 0.05 * lns;
}
double pam(int lns)
{
return 0.03 * lns + 0.0004 * lns * lns;
}
void estimate(int lines, double (*pf)(int))
{
using namespace std;
cout << lines << " lines will take ";
cout << (*pf)(lines) << " hour(s)\n";
}
奇怪 为毛没有颜色 算了不管他
嗯 深入函数指针(变态来一波)
const double* f1(const double ar[], int n)
const double* f2(const double [], int n)
const double* f3(const double*, int n)
这三个函数声明是同一个意思
改成用函数指针表示(直接初始化):
const double* (*p1)(const double*, int n) = f1;
可以使用 auto自动推断类型
auto p2 = f2;
那么 如下调用函数都是OK的 (*p1)(av, 2) , p2(av, 2) (av是一个double数组)
然后:
造门搞一个函数指针数组 指向这3个函数
const double* (*pa[3])(const double*, int) = {f1, f2, f3};
这里表示3个指针元素的函数数组 指向3个函数并返回const double指针
这里不能使用auto 因为auto不能用于初始化列表 只能用于单值初始化
但可以这样 auto pb = pa;
所以 现在pb和pa 都是指向函数指针数组第一个元素的指针(f1)
那么 , 如何用呢?
比如使用数组第二个元素
const double* px = pa[1[(av, 3)(传参)
或者 (*pb[1])(av, 3)(我感觉 (*(pb + 1))(av, 3)也行)
如果要获取返回数据的值
const double x = *pa[1[(av, 3)
const double x = *(*pb[1])(av, 3)
然后再来一个 创建一个指向该函数指针数组的指针
阿西吧~
自动的办法: auto pc = &pa;
手动的办法:
const double* (*(*pd)[3])(const double *, int) = &pa;
解释下
(*pd)[3]表示的是一个数组指针, 指向3个函数指针的数组
*(*pd)[3]表示的是该数组的指针,
const double* 表示的是函数返回的数据类型 因为函数指针必须要跟函数的返回值类型一致
那么在解引用方面
*pd表示的是该函数指针数组,* (*pd)[i](av, 3) 表示的是第i元素所指向的函数(要传参)的返回值数据(const double *)
*(*(*pd)[i])(av, 3) 是得到返回值数据的值(const double)
然后针对数组指针:
**&pa == *pa == pa[0]
因为&pa代表的是整个数组的地址
卧槽 终于理顺了 有点想吐了
示例:
#include <iostream>
const double* f1(const double ar[], int n);
const double* f2(const double [], int);
const double* f3(const double*, int);
int main()
{
using namespace std;
double av[3] = {1112.3, 1542.6, 2227.9};
const double* (*p1)(const double*, int) = f1;
auto p2 = f2;
cout << "Using pointers to functions:\n";
cout << " Address Value\n";
cout << (*p1)(av, 3) << ": " << *(*p1)(av, 3) << endl;
cout << p2(av, 3) << ": " << *p2(av, 3) << endl;
const double* (*pa[3])(const double*, int) = {f1, f2, f3};
auto pb = pa;
cout << "\nUsing an array of pointers to functions:\n";
cout << " Address Vlue\n";
for (int i = 0; i < 3; i++)
cout << pa[i](av, 3) << ": " << *pa[i](av, 3) << endl;
cout << "\nUsing a pointer to a pointer to a function:\n";
cout << " Address Value\n";
for (int i = 0; i < 3; i++)
cout << pb[i](av, 3) << ": " << *pb[i](av, 3) << endl;
cout << "\nUsing pointers to an array of pointers:\n";
cout << " Address Value\n";
auto pc = &pa;
cout << (*pc)[0](av, 3) << ": " << *(*pc)[0](av, 3) << endl;
const double* (*(*pd)[3])(const double*, int) = &pa;
const double* pdb = (*pd)[1](av, 3);
cout << pdb << ": " << *pdb << endl;
cout << (*(*pd)[2])(av, 3) << ": " << *(*(*pd)[2])(av, 3) << endl;
return 0;
}
const double* f1(const double* ar, int n)
{
return ar;
}
const double* f2(const double ar[], int n)
{
return ar + 1;
}
const double* f3(const double ar[], int n)
{
return ar+2;
}
除了使用auto来简化定义变量外还可以使用typedef
typedef const double* (*p_fun)(const double *, int)
p_fun = f1
p_fun pa[3] = {f1, f2, f3}
p_fun (*pd)[3] = &pa
第七章完结。我脑袋可能要休息下