-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwotemp.cpp
57 lines (55 loc) · 1.26 KB
/
twotemp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// twotemps.cpp -- using overloaded template functions
#include <iostream>
template <class Any> // original template
void Swap(Any &a, Any &b);
template <class Any> // new template
void Swap(Any *a, Any *b, int n);
void Show(int a[]);
const int Lim = 8;
int main()
{
using namespace std;
int i = 10, j = 20;
cout << "i, j = " << i << ", " << j << ".\n";
cout << "Using compiler-generated int swapper:\n";
Swap(i,j); // matches original template
cout << "Now i, j = " << i << ", " << j << ".\n";
int d1[Lim] = {0,7,0,4,1,7,7,6};
int d2[Lim] = {0,6,2,0,1,9,6,9};
cout << "Original arrays:\n";
Show(d1);
Show(d2);
Swap(d1,d2,Lim); // matches new template
cout << "Swapped arrays:\n";
Show(d1);
Show(d2);
return 0;
}
template <class Any>
void Swap(Any &a, Any &b)
{
Any temp;
temp = a;
a = b;
b = temp;
}
template <class Any>
void Swap(Any a[], Any b[], int n)
{
Any 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;
}