-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyVector.cpp
More file actions
49 lines (37 loc) · 904 Bytes
/
copyVector.cpp
File metadata and controls
49 lines (37 loc) · 904 Bytes
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
//
// Example of how to use two templates
//
// Week 2, lecture 2.2
//
#include <iostream>
using namespace std;
#include <vector>
//
// Pay attention on the safe casting 'static_cast'
//
/*
. More types means worrying about convertions and more signatures
. static_cast operators are considered safe
. The old cast operator (type) is deprecated
*/
template <class T1, class T2>
void copy(const T1 source[], T2 destination[], int size) {
for (int i = 0; i < size; ++i)
destination[i] = static_cast<T2>(source[i]);
}
template <class T>
T display(T data[], int size) {
for (int i = 0; i<size; ++i)
cout << "Value(" << i << ") = " << data[i] << endl;
return 0;
}
int main() {
cout << "using two templates" << endl;
int a[] = {1, 2, 3};
double b[3];
copy(a, b, 3);
display(b, 3);
double c[] = {3.9, 4.5, 6.7};
copy (c, a, 3);
display(a, 3);
}