Third practice 1

Posted lightice

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Third practice 1相关的知识,希望对你有一定的参考价值。

Third practice 1

任务描述

分别使用传值、引用、指针参数,以及模板函数实现Swap( x, y ),函数功能为交换x、y的值。

测试输入:342.23.3

预期输出:

j = 3 k=4

v = 2.2 w=3.3

After swap:

j = 4 k = 3

v = 3.3 w = 2.2

源代码

#include <iostream>
using namespace std;

template <typename  T>
void Swap(T &x, T &y)
{
	T z;
	z = x;
	x = y;
	y = z;
}
int main()
{
	float j,k,v,w;
	cin>>j>>k>>v>>w;
	cout<<"j = "<<j<<" k = "<<k<<endl;
	cout<<"v = "<<v<<" w = "<<w<<endl;

	Swap(j,k);
	Swap(v,w);

	cout<<"After swap:"<<endl;
	cout<<"j = "<<j<<" k = "<<k<<endl;
	cout<<"v = "<<v<<" w = "<<w;
	return 0;
}


以上是关于Third practice 1的主要内容,如果未能解决你的问题,请参考以下文章

Third practice 2

Third practice 4

Third practice 3

Third practice 6

Third Maximum Number

The third day