Help with function templates

I'm not too good with function templates and was wondering if you guys could help me. I'm trying to change my program to use function templates instead of me writing everything out the way I did. Can you guys show how to turn this into a function template?

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
  #include <iostream>
using namespace std;

void swapA(int &x, int &y);
void swapB(double &x, double &y);
void swapC(char &x, char &y);

int main()
{
	int a = 5;
	int b = 7;

	cout << "Before swap, value of a :" << a << endl;
	cout << "Before swap, value of b :" << b << endl;

	swap(a, b);

	cout << "After swap, value of a :" << a << endl;
	cout << "After swap, value of b :" << b << endl;
	/*************************************************/
	cout << endl;

	double c = 100;
	double d = 450;
	
	cout << "Before swap, value of c :" << c << endl;
	cout << "Before swap, value of d :" << d << endl;

	swap(c, d);

	cout << "After swap, value of c :" << c << endl;
	cout << "After swap, value of d :" << d << endl;
	/*************************************************/
	cout << endl;

	char First = 27;
	char Second = 72;

	cout << "Before swap, value of First :" << int(First) << endl;
	cout << "Before swap, value of Second :" << int(Second) << endl;

	swap(First, Second);

	cout << "After swap, value of First :" << int(First) << endl;
	cout << "After swap, value of Second :" << int(Second) << endl;

	system("pause");
	return 0;
}
The template portion of the code should be something like this
template<class /*or typename*/ T>

After that, replace the parameter and local variable types with T's (The parameters with T &). Just remember when calling the function, you must first call the function name, the type inside <>, then the arguments.
Last edited on
Hi Jessiebnyc,
following the guideline by ForTheReallys in the previous message, the function could be:
1
2
3
4
5
6
template<typename T>
void swap(T& a, T& b){
	T temp = a;
	a = b;
	b = temp;
}


Though, when I tried the code, it clashed with std::swap (see http://www.cplusplus.com/reference/utility/swap/) because of using namespace std; I replaced that with using std::cout; using std::endl; :

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

#include <iostream>
using std::cout;
using std::endl;

template<typename T>
void swap(T& a, T& b){
	T temp = a;
	a = b;
	b = temp;
}

int main(){
	int a = 5;
	int b = 7;

	cout << "Before swap, value of a :" << a << endl;
	cout << "Before swap, value of b :" << b << endl;

	swap(a, b);

	cout << "After swap, value of a :" << a << endl;
	cout << "After swap, value of b :" << b << endl;
	/*************************************************/
	cout << endl;

	double c = 100;
	double d = 450;

	cout << "Before swap, value of c :" << c << endl;
	cout << "Before swap, value of d :" << d << endl;

	swap(c, d);

	cout << "After swap, value of c :" << c << endl;
	cout << "After swap, value of d :" << d << endl;
	/*************************************************/
	cout << endl;

	char First = 27;
	char Second = 72;

	cout << "Before swap, value of First :" << int(First) << endl;
	cout << "Before swap, value of Second :" << int(Second) << endl;

	swap(First, Second);

	cout << "After swap, value of First :" << int(First) << endl;
	cout << "After swap, value of Second :" << int(Second) << endl;

//	system("pause"); // windows only!
	return 0;
}


Regards,
Dean
why would you write your own swap function when the standard provides one?

http://en.cppreference.com/w/cpp/algorithm/swap
Topic archived. No new replies allowed.