#include <iostream>
using
namespace
std;
const
int
num=5;
template
<
class
ElementType>
void
Swap(ElementType a,ElementType b)
{
ElementType temp;
cout<<
"交换前,元素a和元素b的值为:"
<<endl;
cout<<
"a="
<<a<<
"\tb="
<<b<<endl;a
cout<<
"调用Swap(ElementType,ElementType)函数:"
<<endl;
temp=b;
b=a;
a=temp;
cout<<
"a="
<<a<<
"\tb="
<<b<<endl;
}
template
<
class
ElementType>
void
Swap(ElementType a[],ElementType b[],
int
n)
{
ElementType temp;
cout<<
"交换前,数组a[]和数组b[]的值为:"
<<endl;
for
(
int
i=0;i<n;i++)
{
cout<<
"a["
<<i<<
"]为:"
<<a[i]<<
" "
;
}
cout<<endl;
for
(
int
i=0;i<n;i++)
{
cout<<
"b["
<<i<<
"]为:"
<<b[i]<<
" "
;
}
cout<<endl;
cout<<
"调用Swap(ElementType,ElementType,int)函数:"
<<endl;
for
(
int
i=0;i<n;i++)
{
temp=b[i];
b[i]=a[i];
a[i]=temp;
}
for
(
int
i=0;i<n;i++)
{
cout<<
"a["
<<i<<
"]为:"
<<a[i]<<
" "
;
}
cout<<endl;
for
(
int
i=0;i<n;i++)
{
cout<<
"b["
<<i<<
"]为:"
<<b[i]<<
" "
;
}
cout<<endl;
}
int
main()
{
int
x=1,y=2;
Swap(x,y);
int
num1[num]={1,2,3,4,5};
int
num2[num]={6,7,8,9,10};
Swap(num1,num2,num);
return
0;
}