标签:cout temp int void C++ Swap template 重载 模板
#include <iostream>
#include <string>
template <typename T>
void Swap(T &a, T &b);
template <typename T>
void Swap(T[], T[], int);
void show (int *a, int n);
int main()
{
using namespace std;
int a,b;
double x,y;
string p,t;
cout << "please input a(int) and b(int)";
cin >> a >> b;
cout << "now a is " << a << " b is " << b <<endl;
Swap(a,b);
cout << "function Swap after " << "a is " << a << " b is " << b <<endl;
cout << "please input x(double) and y(double)";
cin >> x >> y;
cout << "now x is " << x << "y is " << y <<endl;
Swap(x,y);
cout << "function Swap after " << "x is " << x << " y is " << y <<endl;
cout << "please input p(string) and t(string)";
cin >> p >>t;
cout << "now p is " << p << "t is " << t;
Swap(p,t);
cout << "function Swap after " << "p is " << p << " t is " << t;
cout << "\n模板类重载" <<endl;
int num1[] {1,2,3,4,5};
int num2[] {5,4,3,2,1};
show(num1, 5);
cout << endl;
show(num2, 5);
cout << "Swap after " <<endl;
Swap(num1,num2,5);
show(num1,5);
cout << endl;
show(num2,5);
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)
{
int temp;
for(int i=0;i<n;i++){
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void show(int *a, int n)
{
for(int i=0;i<n;i++){
std::cout << a[i] << " ";
}
}
标签:cout,temp,int,void,C++,Swap,template,重载,模板 来源: https://blog.csdn.net/JAN6055/article/details/122507154
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。