第8章 函数探幽
8.1 C++ 内联函数
在C++中,内联函数是一种用于提升程序执行效率的机制。通过使用 inline 关键字,编译器会尝试将函数调用直接替换为函数体内容,从而避免函数调用带来的开销。
以下是一个展示内联函数使用的示例代码:
#include <iostream>
inline double square(double x) {return x*x;}
int main()
{
using namespace std;
double a, b;
double c = 13.0;
a = square(5.0);
b = square(4.5 + 7.5);
cout << "a= " << a << ", b=" << b << endl;
cout << "c= " << c;
cout << ", c squared = " << square(c++) << endl;
cout << "Now c=" << c << endl;
return 0;
}
该程序的运行输出结果如下:
a= 25, b=144 c= 13, c squared = 169 Now c=14 -------------------------------- Process exited after 0.3357 seconds with return value 0 请按任意键继续. . .[此处为图片1]
8.2 引用变量
引用是C++中一种重要的语言特性,它允许为一个已存在的变量创建一个别名。引用必须在声明时初始化,并且一旦绑定到某个变量后就不能再更改指向。
下面的示例演示了引用的基本用法:
#include <iostream>
int main()
{
using namespace std;
int rats = 101;
int & rodents = rats;
cout << "rats= " << rats << ", rodents=" << rodents << endl;
rodents++;
cout << "rats= " << rats << ", rodents=" << rodents << endl;
cout << "rats address= " << &rats << ", rodents address =" << &rodents << endl;
return 0;
}
程序执行后的输出为:
rats= 101, rodents=101 rats= 102, rodents=102 rats address= 0x6ffe04, rodents address =0x6ffe04 -------------------------------- Process exited after 0.3505 seconds with return value 0 请按任意键继续. . .[此处为图片2]
另一个例子进一步说明引用的行为特点。当引用被初始化后,它始终指向原始变量,即使对引用赋值也只是修改原变量的值,而非改变其引用关系。
#include <iostream>
int main()
{
using namespace std;
int rats = 101;
int & rodents = rats;
cout << "rats= " << rats << ", rodents=" << rodents << endl;
cout << "rats address= " << &rats << ", rodents address =" << &rodents << endl;
int bunnies = 50;
rodents = bunnies;
cout << "bunnies= " << bunnies << ", rats=" << rats << ", rodents= " << rodents << endl;
cout << "bunnies address=" << &bunnies << ", rats address= " << &rats << ", rodents address =" << &rodents << endl;
return 0;
}
对应的运行结果为:
rats= 101, rodents=101 rats address= 0x6ffdf4, rodents address =0x6ffdf4 bunnies= 50, rats=50, rodents= 50 bunnies address=0x6ffdf0, rats address= 0x6ffdf4, rodents address =0x6ffdf4 -------------------------------- Process exited after 0.3265 seconds with return value 0 请按任意键继续. . .[此处为图片3]
环境信息:
- 书籍:C++ Primer Plus(第六版)(中文版)
- 开发工具:Dev-C++ 5.11
- CPU型号:Intel(R) Xeon(R) CPU E5-2603 v3 @ 1.60GHz
- 系统类型:64位操作系统,基于X64的处理器,Windows 10 专业版
实例8.5 cubes.cpp
#include <iostream>
double cube(double a);
double refcube(double &ra);
int main()
{
using namespace std;
double x = 3.0;
cout << cube(x) << " =cube of " << x << endl;
cout << refcube(x) << " =cube of " << x << endl;
return 0;
}
double cube(double a)
{
a *= a * a;
return a;
}
double refcube(double &ra)
{
ra *= ra * ra;
return ra;
}
程序运行结果:
27 =cube of 3
27 =cube of 3
--------------------------------
Process exited after 0.3407 seconds with return value 0
请按任意键继续. . .
实例8.6 strtref.cpp
#include <iostream>
#include <string>
struct free_throws
{
std::string name;
int made;
int attempts;
float percent;
};
void display(const free_throws &ft);
void set_pc(free_throws & ft);
实例8.4 swaps.cpp
#include <iostream>
void swapr(int & a, int & b);
void swapp(int * p, int * q);
void swapv(int a, int b);
int main()
{
using namespace std;
int w1 = 300;
int w2 = 350;
cout << "w1= " << w1 << ", w2=" << w2 << endl;
cout << "using references to swap contents:\n";
//swapr(w1,w2);
cout << "w1= " << w1 << ", w2=" << w2 << endl;
cout << "using pointers to swap contents:\n";
//swapp(&w1,&w2);
cout << "w1= " << w1 << ", w2=" << w2 << endl;
cout << "Trying to use passing by value:\n";
swapv(w1, w2);
cout << "w1= " << w1 << ", w2=" << w2 << endl;
return 0;
}
void swapr(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void swapp(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
void swapv(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
执行输出结果:
w1= 300, w2=350
using references to swap contents:
w1= 300, w2=350
using pointers to swap contents:
w1= 300, w2=350
Trying to use passing by value:
w1= 300, w2=350
--------------------------------
Process exited after 0.3774 seconds with return value 0
请按任意键继续. . .
bunnies address=0x6ffdf0, rats address= 0x6ffdf4, rodents address =0x6ffdf4
#include <iostream>
#include <string>
using namespace std;
struct free_throws {
string name;
int made;
int attempts;
float percent;
};
void display(const free_throws &ft);
void set_pc(free_throws &ft);
free_throws &accumulate(free_throws &target, const free_throws &source);
int main()
{
free_throws one = {"Branch", 13, 14};
free_throws two = {"Knot", 10, 16};
free_throws three = {"Max", 7, 9};
free_throws four = {"Looper", 5, 9};
free_throws five = {"Long", 6, 14};
free_throws team = {"Goods", 0, 0};
free_throws dup;
set_pc(one);
display(one);
accumulate(team, one);
display(team);
display(accumulate(team, two));
accumulate(accumulate(team, three), four);
display(team);
dup = accumulate(team, five);
std::cout << "Display team:\n";
display(team);
std::cout << "Displaying dup after assignment:\n";
display(dup);
set_pc(four);
accumulate(dup, five) = four;
std::cout << "Displaying dupp after ill-advised assignment:\n";
display(dup);
return 0;
}
void display(const free_throws &ft)
{
using std::cout;
cout << "name: " << ft.name << '\n';
cout << " made: " << ft.made << '\t';
cout << " Attempts: " << ft.attempts << '\t';
cout << " percent: " << ft.percent << '\n';
}
void set_pc(free_throws &ft)
{
if (ft.attempts != 0)
ft.percent = 100.0 * float(ft.made) / float(ft.attempts);
else
ft.percent = 0;
}
free_throws &accumulate(free_throws &target, const free_throws &source)
{
target.attempts += source.attempts;
target.made += source.made;
set_pc(target);
return target;
}
运行输出结果如下所示:
name: Branch made: 13 Attempts: 14 percent: 92.8571 name: Goods made: 13 Attempts: 14 percent: 92.8571 name: Goods made: 23 Attempts: 30 percent: 76.6667 name: Goods made: 35 Attempts: 48 percent: 72.9167 Display team: name: Goods made: 41 Attempts: 62 percent: 66.129 Displaying dup after assignment: name: Goods made: 41 Attempts: 62 percent: 66.129 Displaying dupp after ill-advised assignment: name: Looper made: 5 Attempts: 9 percent: 55.5556 -------------------------------- Process exited after 0.3639 seconds with return value 0 请按任意键继续. . .[此处为图片1] 该程序演示了结构体与函数引用的结合使用,重点在于通过引用传递来高效地修改和累积数据。accumulate 函数接收两个参数:目标对象和源对象,并将源对象的投篮次数和命中数累加到目标对象中,同时更新命中率。display 函数用于格式化输出每个球员的数据信息。set_pc 函数则负责计算并设置命中百分比。 在主函数中,首先初始化多个 free_throws 类型的变量,代表不同球员的罚球统计。随后调用 set_pc 和 accumulate 等函数进行数据处理,并逐步输出中间结果。最后展示了对引用返回值进行错误赋值所导致的异常行为,强调了合理使用引用的重要性。
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int LIMIT = 5;
void file_it(ostream &os, double fo, const double fe[], int n);
int main()
{
ofstream fout;
const char *fn = "ep-data.txt";
fout.open(fn);
if (!fout.is_open())
{
cout << "Can't open " << fn << ". Bye.\n";
exit(EXIT_FAILURE);
}
double objective;
该程序的主要功能是向指定文件写入格式化数据。通过定义一个输出流参数,函数 file_it 能够灵活地将信息输出到不同目标,例如文件或标准输出设备。
string version1(const string &s1, const string &s2);
const string &version2(string &s1, const string &s2);
const string &version3(string &s1, const string &s2);
int main()
{
string input;
string copy;
string result;
cout << "Enter a string: ";
getline(cin, input);
copy = input;
cout << "Your string as entered: " << input << endl;
result = version1(input, "***");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;
result = version2(input, "###");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;
cout << "Resetting original string:\n";
input = copy;
result = version3(input, "@@@");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;
return 0;
}
string version1(const string &s1, const string &s2)
{
string temp;
temp = s2 + s1 + s2;
return temp;
}
const string &version2(string &s1, const string &s2)
{
s1 = s2 + s1 + s2;
return s1;
}
const string &version3(string &s1, const string &s2)
{
string temp;
temp = s2 + s1 + s2;
return temp;
}
运行输出结果如下所示:
Enter a string: It's not my fault. Your string as entered: It's not my fault. Your string enhanced: ***It's not my fault.*** Your original string: It's not my fault. Your string enhanced: ###It's not my fault.### Your original string: ###It's not my fault.### Resetting original string:[此处为图片1]
-------------------------------- Process exited after 18.19 seconds with return value 3221226356 请按任意键继续. . .
示例代码 filefunc.cpp 展示了如何使用文件流进行数据存储操作。其中,函数 file_it 接收一个通用的输出流引用,使得它既能输出到文件,也可用于屏幕显示,提高了代码复用性。
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int LIMIT = 5;
void file_it(ostream &os, double fo, const double fe[], int n);
int main()
{
double objective;
cout << "Enter the focal length of your telescope objective in mm: ";
cin >> objective;
double eps[LIMIT];
cout << "Enter the focal lengths, in mm, of " << LIMIT << " eyepieces:\n";
for (int i = 0; i < LIMIT; i++)
{
cout << "Eyepiece #" << i + 1 << ": ";
cin >> eps[i];
}
ofstream fout("output.txt");
if (fout.is_open())
{
file_it(fout, objective, eps, LIMIT);
fout.close();
}
file_it(cout, objective, eps, LIMIT);
cout << "Done.\n";
return 0;
}
void file_it(ostream &os, double fo, const double fe[], int n)
{
ios_base::fmtflags initial;
initial = os.setf(ios_base::fixed);
os.precision(0);
os << "Focal length of objective: " << fo << "mm\n";
os.setf(ios::showpoint);
os.precision(1);
os.width(12);
os << "f.1. eyepiece";
os.width(15);
os << "magnification" << endl;
for (int i = 0; i < n; i++)
{
os.width(12);
os << fe[i];
os.width(15);
os << int(fo / fe[i] + 0.5) << endl;
}
os.setf(initial);
}
程序执行输出示例:
Enter the focal length of your telescope objective in mm: 1800
Enter the focal lengths, in mm, of 5 eyepieces:
Eyepiece #1: 30
Eyepiece #2: 19
Eyepiece #3: 14
Eyepiece #4: 8.9
Eyepiece #5: 7.5
Focal length of objective: 1800mm
f.1. eyepiece magnification
30.0 60
19.0 95
14.0 129
8.9 202
7.5 240
Done.
[此处为图片1]
程序运行结束,返回值为 0。
执行耗时约 25.58 秒。
请按任意键继续...

雷达卡


京公网安备 11010802022788号







