C++ primer plus chapter 8 函数探幽

内联函数

内联函数编译时将用函数定义替换函数调用,这样,程序就不需要跳转到另外的地址执行代码再跳回来,而普通函数执行时需要保存其跳转时的地址,因此内联函数执行速度比普通函数稍快,但是会占用更多内存。如果程序在10个不同的地方使用内联函数则该程序会包含该函数的10个副本。

#include <iostream>

using namespace std;

inline double square(double x) {return x*x;}//函数定义充当函数原型

int main(void)
{
	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 << endl;
	cout << "c square = " << square(c++) << endl;
	cout << "Now c = " << c << endl;

	return 0;
}

如果函数执行时间很短则使用内联函数能节省大量时间

 

简短函数尽量用内联函数而不是函数宏

引用变量 如同char* 表示指向char类型的指针一样 int& 表示int类型变量的引用 必须在声明引用时将其初始化 而不是先声明再赋值 如同const指针再创建时就初始化一样

#include <iostream>

using namespace std;

int main(void)
{
	int rats = 10;
	int &rodents = rats;

	cout << "rats = " << rats << endl;
	cout << "rodents = " << rodents << endl;

	rodents++;
	cout << "rats = " << rats << endl;
	cout << "rodents = " << rodents << endl;
	
	cout << "&rats = " << &rats << endl;
	cout << "&rodents = " << &rodents << endl;

	return 0;
}

#include <iostream>

using namespace std;

int main(void)
{
	int rats = 101;
	int &rodents = rats;

	cout << "rats = " << rats << endl;
	cout << "rodents = " << rodents << endl;

	cout << "&rats = " << &rats << endl;
	cout << "&rodents = " << &rodents << endl;

	int bunnies = 50;//此处时赋值操作 而不是修改引用关系
	rodents = bunnies;
	cout << "bunnies = " << bunnies << endl;
	cout << "rodents = " << rodents << endl;
	cout << "rats = " << rats << endl;

	cout << "&bunnies  = " << &bunnies << endl;
	cout << "&rodents = " << &rodents << endl;

	return 0;
}

引用作用与函数参数

#include <iostream>

using namespace std;

void swapr(int &a, int &b);
void swapp(int *pa, int *pb);
void swapv(int a, int b);

int main(void)
{
	int wallet1 = 300;
	int wallet2 = 350;

	cout << "wallet1 = " << wallet1 << endl;
	cout << "wallet2 = " << wallet2 << endl;

	cout << "Using reference to swap contents:" << endl;
	swapr(wallet1, wallet2);
	cout << "wallet1 = " << wallet1 << endl;
	cout << "wallet2 = " << wallet2 << endl;

	cout << "Using pointers to swap contents:" << endl;
	swapp(&wallet1, &wallet2);
	cout << "wallet1 = " << wallet1 << endl;
	cout << "wallet2 = " << wallet2 << endl;

	cout << "Trying to use passing by value:" << endl;
	swapv(wallet1, wallet2);
	cout << "wallet1 = " << wallet1 << endl;
	cout << "wallet2 = " << wallet2 << endl;

	return 0;
}

void swapr(int &a, int &b)
{
	int temp;

	temp = a;
	a = b;
	b = temp;
}

void swapp(int *pa, int *pb)
{
	int temp;

	temp = *pa;
	*pa = *pb;
	*pb = temp;
}

void swapv(int a, int b)
{
	int temp;

	temp = a;
	a = b;
	b = temp;
}
#include <iostream>

using namespace std;

double cube(double a);
double recube(double &ra);

int main(void)
{
	double x = 3.0;

	cout << cube(x) << " = cube of " << x << endl;
	
	cout << recube(x) << " = cube of " << x << endl;

	return 0;
}

double cube(double a)
{
	a *= a * a;
	return a;
}

double recube(const double &ra)
{
	ra *= ra * ra;
	return ra;
}

引用是变量的别名故实参应该是变量而不是常量或者常量表达式 当实参和引用参数不匹配且参数为const引用时 将生成临时变量 然后引用临时变量

#include <iostream>

using namespace std;

double cube(double a);
double recube(double &ra);

int main(void)
{
	double x = 3.0;
	const double y = 5.0;

	cout << cube(5.0 + x) << " = cube of " << "5.0 + x" << endl;
	
	cout << recube(y) << " = cube of " << y << endl;

	return 0;
}

double cube(double a)
{
	a *= a * a;
	return a;
}

double recube(double &ra)
{
	return ra * ra * ra;
}

引用用于结构体

#include <iostream>
#include <string>

using namespace std;

struct free_throws
{
	string name;
	int made;
	int attempts;
	float percent;
};

void set_pc(free_throws &ft);
void display(const free_throws &ft);
free_throws &accumulate(free_throws &target, const free_throws &source);

int main(void)
{
	free_throws one = {"Rick", 13, 14};
	free_throws two = {"Jack", 10, 16};
	free_throws three = {"Jerry", 7, 9};
	free_throws four = {"Jason", 5, 9};
	free_throws five = {"Micheal", 6, 14};
	free_throws team = {"Class 6", 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);
	cout << "Display team:" << endl;
	display(team);
	display(dup);

	return 0;
}

void set_pc(free_throws &ft)
{
	if(ft.attempts != 0)
		ft.percent = 100.0 * float(ft.made) / float(ft.attempts);
	else
		ft.attempts = 0;
}

void display(const free_throws &ft)
{
	cout << "Name: " << ft.name << endl;
	cout << "Made: " << ft.made << '\t';
	cout << "Attempts: " << ft.attempts << '\t';
	cout << "Percent: " << ft.percent << endl;
}

free_throws &accumulate(free_throws &target, const free_throws &source)
{
	target.attempts += source.attempts;
	target.made += source.made;
	set_pc(target);
	return target;
}

#include <iostream>
#include <string>

using namespace std;

struct free_throws
{
	string name;
	int made;
	int attempts;
	float percent;
};

void set_pc(free_throws &ft);
void display(const free_throws &ft);
free_throws &accumulate(free_throws &target, const free_throws &source);

int main(void)
{
	free_throws one = {"Rick", 13, 14};//如果指定的初始值比成员少 余下的成员就被赋值为0
	free_throws two = {"Jack", 10, 16};
	free_throws three = {"Jerry", 7, 9};
	free_throws four = {"Jason", 5, 9};
	free_throws five = {"Micheal", 6, 14};
	free_throws team = {"Class 6", 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);
	cout << "Display team:" << endl;
	display(team);
	display(dup);

	return 0;
}

void set_pc(free_throws &ft)
{
	if(ft.attempts != 0)
		ft.percent = 100.0 * float(ft.made) / float(ft.attempts);
	else
		ft.attempts = 0;
}

void display(const free_throws &ft)
{
	cout << "Name: " << ft.name << endl;
	cout << "Made: " << ft.made << '\t';
	cout << "Attempts: " << ft.attempts << '\t';
	cout << "Percent: " << ft.percent << endl;
}

free_throws &accumulate(free_throws &target, const free_throws &source)
{
	target.attempts += source.attempts;
	target.made += source.made;
	set_pc(target);
	return target;//光看返回类型不知道是返回引用 但函数头和原型指出了这一点 
//如果返回类型是free_throws 而不是free_throws &则上述将返回拷贝
}

 

 

 

 类的引用

#include <iostream>
#include <string>

using namespace std;

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(void)
{
	string input;
	string result;
	string copy;

	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;
	
	cout << "-----------------------------------" << endl;

	result = version2(input, "###");
	cout << "Your string enhanced: " << result << endl;
	cout << "Your original string: " << input << endl;

	cout << "-----------------------------------" << endl;
		
	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;
}

对象继承和引用

基类的引用可以指向派生类对象

函数的形参里通过基类的引用分别指向基类的对象和派生类的对象

#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(void)
{
	fstream fout;
	const char *fn = "ep-data.txt";

	fout.open(fn);
	if(!fout.is_open())
	{
		cout << "Can't open " << fn << ". Bye." << endl;
		exit(EXIT_FAILURE);
	}

	double objective;
	cout << "Enter the focal length of your telescope objective in mm:";
	cin >> objective;

	double eps[LIMIT];
	for(int i = 0; i < LIMIT; i ++)
	{
		cout << "EyePieces #" << i + 1 << ": ";
		cin >> eps[i];
	}

	file_it(cout, objective, eps, LIMIT);
	file_it(fout, objective, eps, LIMIT);

	cout << "Done." << endl;

	return 0;
}

void file_it(ostream &os, double fo, const double fe[], int n)
{
	os << "Focal length of objective: " << fo << endl;
	os << "f.1. eyepieces" << " magnification" << endl;
	for(int i = 0; i < n; i++)
	{
		os << "        " << fe[i] << "       " << int(fo/fe[i] + 0.5) << endl;
	}
}

默认参数

由于编译器通过查看原型来了解函数所使用的参数数目 因此默认参数应该在函数原型中体现

函数定义不变

#include <iostream>

using namespace std;

const int ArSize = 80;

char *left(const char *str, int n = 1);

int main(void)
{
	char sample[ArSize];

	cout << "Enter a string: " << endl;
	cin.get(sample, ArSize);

	char *ps = left(sample, 400);
	cout << ps << endl;
	delete []ps;

	ps = left(sample);
	cout << ps << endl;
	delete []ps;

	return 0;
}

/*
char *left(const char *str, int n)
{
	if(n < 0)
		n = 0;

	char *p = new char[n+1];

	int i;

	for(i = 0; i < n && str[i]; i++)
		p[i] = str[i];

	while(i <= n)
		p[i++] = '\0';

	return p;
}
*/

char *left(const char *str, int n)
{
	int m = 0;
	while(m < n && str[m] != '\0')
		m++;

	char *p = new char[n+1];

	int i;
	for(i = 0; i < m; i++)
		p[i] = str[i];

	p[i] = '\0';

	return p;
}

左值右值与左值引用和右值引用

#include <iostream>

using namespace std;

int main(void)
{
	int a = 10;
	int b = 20;
	
//	int &c = a;	
	//int &d = 10;
	//int &d = (a+b);

//	const int &d = 10;
//	const int &c = (a+b);

	int &&x = 10;
	int &&y = (a+b);//右值此时会计算并赋值到一个临时变量中 再将临时变量赋值给引用
    //但是这样的引用不能修改被引用的内容 因此C++引入了右值引用
	return 0;
}

/*
	left ,right = , +=
*/

函数重载——函数重载的关键是函数的参数列表(函数特征标)参数类型 数量 顺序 注意变量和变量引用编译器不会区分 因为这两种情况可以传入相同参数 故编译器把类型引用和类型本身视为一个特征标

 编译器认为第一种情况是重复定义

#include <iostream>

using namespace std;

const int ArSize = 80;

char *left(const char *str, int n = 1);
unsigned long left(unsigned long num, unsigned int ct);

int main(void)
{
	const char *trip = "Hawaii";
	unsigned long n = 12345678;
	int i;
	char *temp;

	for(i = 0; i < 10; i++)
	{
		cout << left(n, i) << endl;
		temp = left(trip, i);
		cout << temp << endl;
		delete []temp;
	}

	return 0;
}

char *left(const char *str, int n)
{
	int m = 0;
	while(m < n && str[m] != '\0')
		m++;

	char *p = new char[n+1];

	int i;
	for(i = 0; i < m; i++)
		p[i] = str[i];

	p[i] = '\0';

	return p;
}

unsigned long left(unsigned long num, unsigned int ct)
{
	unsigned long n = num;
	unsigned int digits = 1;

	if(num == 0 || ct == 0)
		return 0;

	while(n /= 10)
		digits++;

	if(digits > ct)
	{
		ct = digits - ct;
		while(ct--)
			num /= 10;
		return num;
	}
	else
		return num;
}

函数模板

#include <iostream>

using namespace std;

template <typename T>
void Swap(T &a, T &b);

int main(void)
{
	int i = 10;
	int j = 20;

	cout << "i, j = " << i << ", " << j << "." << endl;
	Swap(i, j);
	cout << "After swap, now i, j = " << i << ", " << j << "." << endl;

	double x = 24.5;
	double y = 81.7;
	cout << "x, y = " << x << ", " << y << "." << endl;
	Swap(x, y);
	cout << "After swap, now x, y = " << x << ", " << y << "." << endl;

	return 0;
}

template <typename T>
void Swap(T &a, T &b)
{
	T temp;
	temp = a;
	a = b;
	b = temp;
}

 重载的模板

#include <iostream>

using namespace std;

template <typename T>
void Swap(T &a, T &b);

const int LIM = 8;

template <typename T>
void Swap(T a[], T b[], int n);

void show(int arr[], int n);

int main(void)
{
	int i = 10;
	int j = 20;

	cout << "i, j = " << i << ", " << j << "." << endl;
	swap(i, j);
	cout << "After swap, now i, j = " << i << ", " << j << "." << endl;

	int d1[LIM] = {0, 7, 0, 4, 1, 7, 7, 6};
	int d2[LIM] = {0, 7, 2, 0, 1, 9, 6, 9};
	cout << "Origianl arrays: " << endl;
	show(d1, LIM);
	show(d2, LIM);
	Swap(d1, d2, LIM);
	cout << "Swapped arrays: " << endl;
	show(d1, LIM);
	show(d2, LIM);

	return 0;
}

template <typename T>
void Swap(T &a, T &b)
{
	T temp;
	temp = a;
	a = b;
	b = temp;
}

template <typename T>
void Swap(T a[], T b[], int n)
{
	T temp;
	for(int i = 0; i < n; i++)
	{
		temp = a[i];
		a[i] = b[i];
		b[i] = temp;
	}
}

void show(int arr[], int n)
{
	for(int i = 0; i < n; i++)
		cout << arr[i] << " ";
	cout << endl;
}

模板的局限性——可以显示具体化(对某一特定数据类型做单独处理,而特征标相同无法使用函数重载时用显示具体化)

非模板函数>显示具体化模板函数>常规模板函数

#include <iostream>

using namespace std;

struct job
{
	char name[40];
	double salary;
	int floor;
};

template <typename T>
void Swap(T &a, T &b);

template <typename T>
void Swap(T &a, T &b);

template <> void Swap<job>(job &j1, job &j2);

void show(job &j);

int main(void)
{
	int i = 10;
	int j = 20;

	cout << "i, j = " << i << ", " << j << "." << endl;
	Swap(i, j);
	cout << "After swap, now i, j = " << i << ", " << j << "." << endl;

	double x = 24.5;
	double y = 81.7;
	cout << "x, y = " << x << ", " << y << "." << endl;
	Swap(x, y);
	cout << "After swap, now x, y = " << x << ", " << y << "." << endl;

	job Rick = {"Rick", 1000, 10};
	job Jack = {"Jack", 1100, 11};
	show(Rick);
	show(Jack);
	Swap(Rick, Jack);
	cout << "After swap: " << endl;
	show(Rick);
	show(Jack);

	return 0;
}

template <typename T>
void Swap(T &a, T &b)
{
	T temp;
	temp = a;
	a = b;
	b = temp;
}

template <> void Swap<job>(job &j1, job &j2)
{
	double t1;
	int t2;

	t1 = j1.salary;
	j1.salary = j2.salary;
	j2.salary = t1;

	t2 = j1.floor;
	j1.floor = j2.floor;
	j2.floor = t2;
}

void show(job &j)
{
	cout << j.name << ": " << j.salary << " on floor " << j.floor << endl;
}
#include <iostream>

using namespace std;

struct job
{
	char name[40];
	double salary;
	int floor;
};

void Swap(int a, int b);

template <typename T>
void Swap(T &a, T &b);

//template <typename T>
//void Swap(T &a, T &b);

template <> void Swap<job>(job &j1, job &j2);

void show(job &j);

int main(void)
{
	int i = 10;
	int j = 20;

	cout << "i, j = " << i << ", " << j << "." << endl;
	Swap(i, j);
	cout << "After swap, now i, j = " << i << ", " << j << "." << endl;

	double x = 24.5;
	double y = 81.7;
	cout << "x, y = " << x << ", " << y << "." << endl;
	Swap(x, y);
	cout << "After swap, now x, y = " << x << ", " << y << "." << endl;

	job Rick = {"Rick", 1000, 10};
	job Jack = {"Jack", 1100, 11};
	show(Rick);
	show(Jack);
	Swap(Rick, Jack);
	cout << "After swap: " << endl;
	show(Rick);
	show(Jack);

	return 0;
}

void Swap(int a, int b)
{
	cout << "Hello world, a = " << a << ", b = " << b << endl;
}

template <typename T>
void Swap(T &a, T &b)
{
	T temp;
	temp = a;
	a = b;
	b = temp;
}

template <> void Swap<job>(job &j1, job &j2)
{
	double t1;
	int t2;

	t1 = j1.salary;
	j1.salary = j2.salary;
	j2.salary = t1;

	t2 = j1.floor;
	j1.floor = j2.floor;
	j2.floor = t2;
}

void show(job &j)
{
	cout << j.name << ": " << j.salary << " on floor " << j.floor << endl;
}

模板的实例化(编译器为特定数据类型生成函数定义时,得到的是函数实例)和具体化

 这两者区别在于显示实例化无需调用也生成函数定义 而具体化必须调用后才有函数定义

 

编译器函数版本的选择

#include <iostream>

using namespace std;

template <typename T>
void ShowArray(T arr[], int n);

template <typename T>
void ShowArray(T *arr[], int n);

struct debts
{
	char name[50];
	double amount;
};

int main(void)
{
	int things[6] = {13, 31, 103, 301, 310, 130};

	struct debts mr_E[3] = 
	{
		{"Rick", 2400.00},
		{"Jack", 1300.0},
		{"Rose", 1800.0}
	};
	double *pd[3];

	for(int i = 0; i < 3; i++)
		pd[i] = &mr_E[i].amount;


	ShowArray(things, 6);

	ShowArray(pd, 3);

	return 0;
}

template <typename T>
void ShowArray(T arr[], int n)
{
	cout << "template A:" << endl;

	for(int i = 0; i < n; i++)
		cout << arr[i] << ' ';

	cout << endl;
}

template <typename T>
void ShowArray(T *arr[], int n)
{
	cout << "template B:" << endl;

	for(int i = 0; i < n; i++)
		cout << *arr[i] << ' ';

	cout << endl;
}

自己选择调用函数

#include <iostream>

using namespace std;

template <class T>
T lesser(T a, T b)
{
	return a < b ? a : b;
}

int lesser(int a, int b)
{
	a = a < 0 ? -a : a;
	b = b < 0 ? -b : b;

	return a < b ? a : b;
}

int main(void)
{
	int m = 20;
	int n = -30;

	double x = 15.5;
	double y = 25.9;

	cout << lesser(m, n) << endl;
	cout << lesser(x, y) << endl;

	cout << lesser<>(m, n) << endl;//强制调用模板函数
	cout << lesser<int>(x, y) << endl;//把参数强制转换为init类型并且代入模板函数

	return 0;
}

关键字decltype看8.5.1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值