参考书籍:C++ Primer Plus(第六版)中文版本
开发工具:Dev-C++ 5.11
设备配置:Intel(R) Xeon(R) CPU E5-2603 v3 @ 1.60GHz
系统信息:Windows 10 专业版,64位操作系统,基于X64架构的处理器
第8章 函数探幽
8.5 函数模板
以下为实例8.11的代码实现,文件名为 funtemp.cpp:
#include <iostream>
template <class T>
void Swap(T &a, T &b);
int main()
{
using namespace std;
int i = 10;
int j = 20;
cout << "i,j = " << i << ", " << j << ".\n";
cout << "Using compiler generated in swapper:\n";
Swap(i, j);
cout << "Now i,j=" << i << ", " << j << ".\n";
double x = 24.5;
double y = 81.7;
cout << "x,y= " << x << ", " << y << ".\n";
cout << "Using compiler generated double swapper:\n";
Swap(x, y);
cout << "Now x,y =" << x << ", " << y << ".\n";
return 0;
}
template <class T>
void Swap(T &a, T &b)
{
T temp;
temp = a;
a = b;
b = temp;
}
程序执行后的输出结果如下:
i,j = 10, 20. Using compiler generated in swapper: Now i,j=20, 10. x,y= 24.5, 81.7. Using compiler generated double swapper: Now x,y =81.7, 24.5. -------------------------------- Process exited after 0.3397 seconds with return value 0 请按任意键继续. . .[此处为图片1]
接下来是另一个版本的示例代码,即实例8.11a,同样命名为 funtemp.cpp,其功能一致,仅在语法细节上略有不同:
#include <iostream>
template <typename T>
void Swap(T &a, T &b);
int main()
{
using namespace std;
int i = 10;
int j = 20;
cout << "i,j = " << i << ", " << j << ".\n";
cout << "Using compiler generated in swapper:\n";
Swap(i, j);
cout << "Now i,j=" << i << ", " << j << ".\n";
double x = 24.5;
double y = 81.7;
cout << "x,y= " << x << ", " << y << ".\n";
cout << "Using compiler generated double swapper:\n";
Swap(x, y);
cout << "Now x,y =" << x << ", " << y << ".\n";
return 0;
}
template <typename T>
void Swap(T &a, T &b)
{
T temp;
temp = a;
a = b;
b = temp;
}
该版本的运行结果与前一版本完全相同:
i,j = 10, 20. Using compiler generated in swapper: Now i,j=20, 10. x,y= 24.5, 81.7. Using compiler generated double swapper: Now x,y =81.7, 24.5. --------------------------------[此处为图片2]
实例8.12 twotemps.cpp
#include <iostream>
template <typename T>
void swap(T &a, T &b);
template <typename T>
void swap(T *a, T *b, int n);
void show(int a[]);
const int Lim = 8;
int main()
{
using namespace std;
int i = 10;
int j = 20;
cout << "i,j = " << i << ", " << j << ".\n";
cout << "Using compiler generated in swapper:\n";
::swap(i, j);
cout << "Now i,j=" << i << ", " << j << ".\n";
int d1[Lim] = {0, 7, 0, 4, 1, 7, 7, 6};
int d2[Lim] = {0, 7, 2, 0, 1, 9, 6, 9};
cout << "original arraya:\n";
show(d1);
show(d2);
::swap(d1, d2, Lim);
cout << "swapped arrays:\n";
show(d1);
show(d2);
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 a[])
{
using namespace std;
cout << a[0] << a[1] << "/";
cout << a[2] << a[3] << "/";
for (int i = 4; i < Lim; i++)
cout << a[i];
cout << endl;
}
程序运行输出结果如下:
i,j = 10, 20.
Using compiler generated in swapper:
Now i,j=20, 10.
original arraya:
07/04/1776
07/20/1969
swapped arrays:
07/20/1969
07/04/1776
执行过程结束,返回值为0。
请按任意键继续...
[此处为图片1]
另一个相似实现版本(实例8.12a)中,函数名由swap改为Swap,并去除了作用域解析符::的显式调用,其余结构保持一致。该版本同样实现了对基本类型变量和数组元素的交换功能。
#include <iostream>
template <typename T>
void Swap(T &a, T &b);
template <typename T>
void Swap(T *a, T *b, int n);
void show(int a[]);
const int Lim = 8;
int main()
{
using namespace std;
int i = 10;
int j = 20;
cout << "i,j = " << i << ", " << j << ".\n";
cout << "Using compiler generated in swapper:\n";
Swap(i, j);
cout << "Now i,j=" << i << ", " << j << ".\n";
int d1[Lim] = {0, 7, 0, 4, 1, 7, 7, 6};
int d2[Lim] = {0, 7, 2, 0, 1, 9, 6, 9};
cout << "original arraya:\n";
show(d1);
show(d2);
Swap(d1, d2, Lim);
cout << "swapped arrays:\n";
show(d1);
show(d2);
return 0;
}
template <typename T>
void Swap(T &a, T &b)
{
T temp;
temp = a;
a = b;
b = temp;
}
此版本中的模板函数通过重载支持两种操作:引用类型的单个变量交换与指针形式的数组批量交换。函数show()用于格式化输出日期样式的整型数组,前四位表示月日,后四位表示年份信息。
两次运行均正常退出,耗时分别为0.2825秒和0.1926秒,系统返回值均为0,表明程序无异常终止。
#include <iostream>
template <typename T>
void Swap(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
struct job
{
char name[40];
double salary;
int floor;
};
template <> void Swap<job>(job &j1, job &j2);
void show(job &j);
int main()
{
using namespace std;
cout.precision(2);
cout.setf(ios::fixed, ios::floatfield);
int i = 10;
int j = 20;
cout << "i,j = " << i << ", " << j << ".\n";
cout << "Using compiler generated in swapper:\n";
Swap(i, j);
cout << "Now i,j=" << i << ", " << j << ".\n";
job sue = {"Susan yaffee", 73000.60, 7};
job sidney = {"Sidney Taffee", 78090.67, 9};
cout << "Before job swapping:\n";
cout << "original arraya:\n";
show(sue);
show(sidney);
Swap(sue, sidney);
cout << "After job swapping:\n";
show(sue);
show(sidney);
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 = j1.salary;
j1.salary = j2.salary;
j2.salary = t1;
int t2 = j1.floor;
j1.floor = j2.floor;
j2.floor = t2;
}
void show(job &j)
{
using namespace std;
cout << j.name << ": $" << j.salary << " on floor " << j.floor << endl;
}
编译运行结果:
i,j = 10, 20.
Using compiler generated in swapper:
Now i,j=20, 10.
Before job swapping:
original arraya:
Susan yaffee: $73000.60 on floor 7
Sidney Taffee: $78090.67 on floor 9
After job swapping:
Susan yaffee: $78090.67 on floor 9
Sidney Taffee: $73000.60 on floor 7
--------------------------------
Process exited after 0.2238 seconds with return value 0
请按任意键继续. . .
[此处为图片1]
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;
}
}
[此处为图片2]
void show(int a[])
{
using namespace std;
cout << a[0] << a[1] << "/";
cout << a[2] << a[3] << "/";
for (int i = 4; i < Lim; ++i)
cout << a[i];
cout << endl;
}
[此处为图片3]
实例8.14 temptempover.cpp
#include <iostream>
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()
{
using namespace std;
int things[6] = {13, 31, 103, 301, 310, 130};
struct debts mr_E[3] =
{
{"Ima wolfe", 2400.0},
{"Ura Foxe", 1300.0},
{"Iby Stout", 1800.0}
};
double *pd[3];
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Listing Mr. E's counts of things:\n";
ShowArray(things, 6);
cout << "Listing Mr. E's counts of debts:\n";
ShowArray(pd, 3);
return 0;
}
template <typename T>
void ShowArray(T arr[], int n)
{
using namespace std;
cout << "template A\n";
for (int i = 0; i < n; i++)
cout << arr[i] << ' ';
cout << endl;
}
template <typename T>
void ShowArray(T *arr[], int n)
{
using namespace std;
cout << "template B\n";
for (int i = 0; i < n; i++)
cout << *arr[i] << ' ';
cout << endl;
}
编译运行结果:
Listing Mr. E's counts of things:
template A
13 31 103 301 310 130
Listing Mr. E's counts of debts:
template B
2400 1300 1800
实例8.15 choices.cpp
#include <iostream>
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()
{
using namespace std;
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;
return 0;
}
编译运行结果:
20
15.5
-30
15


雷达卡


京公网安备 11010802022788号







