// twotemps.cpp -- using overloaded template functions
#include <iostream>
template <typename T> // original template
void Swap(T &a, T &b);
template <typename T> // new template
void Swap(T *a, T *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,7,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);
// cin.get();
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;
}
Ly8gdHdvdGVtcHMuY3BwIC0tIHVzaW5nIG92ZXJsb2FkZWQgdGVtcGxhdGUgZnVuY3Rpb25zCiNpbmNsdWRlIDxpb3N0cmVhbT4KdGVtcGxhdGUgPHR5cGVuYW1lIFQ+ICAgICAvLyBvcmlnaW5hbCB0ZW1wbGF0ZQp2b2lkIFN3YXAoVCAmYSwgVCAmYik7Cgp0ZW1wbGF0ZSA8dHlwZW5hbWUgVD4gICAgIC8vIG5ldyB0ZW1wbGF0ZQp2b2lkIFN3YXAoVCAqYSwgVCAqYiwgaW50IG4pOwoKdm9pZCBTaG93KGludCBhW10pOwpjb25zdCBpbnQgTGltID0gODsKaW50IG1haW4oKQp7CiAgICB1c2luZyBuYW1lc3BhY2Ugc3RkOwogICAgaW50IGkgPSAxMCwgaiA9IDIwOwogICAgY291dCA8PCAiaSwgaiA9ICIgPDwgaSA8PCAi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