Skip to content

Commit b625827

Browse files
committed
-> more practice on templates
1 parent e9966de commit b625827

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

practice10.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
using namespace std;
55

6+
// passing arr via ref - This is the syntax for
7+
// passing arr via reference.
8+
9+
// (&arr)[n] tells the compiler that we want to
10+
// reference an array.
11+
612
template <typename T, typename S, size_t n>
713
void preview(T (&arr)[n], S name)
814
{
@@ -22,6 +28,18 @@ void preview(T (&arr)[n], S name)
2228
}
2329
}
2430

31+
// if "size" is not important.
32+
template <typename T>
33+
void preview2(T *arr, size_t size)
34+
{
35+
for (size_t i = 0; i < size; i++)
36+
{
37+
cout << "arr[" << i << "]: " << arr[i] << endl;
38+
}
39+
40+
cout << endl;
41+
}
42+
2543
int main()
2644
{
2745
int arr_i[] = {1, 2, 3, 4, 5};
@@ -30,5 +48,8 @@ int main()
3048
preview(arr_c, "name1");
3149
preview(arr_i, "name22");
3250

51+
preview2(arr_c, sizeof(arr_c) / sizeof(arr_c[0]));
52+
preview2(arr_i, sizeof(arr_i) / sizeof(arr_i[0]));
53+
3354
return 1;
3455
}

0 commit comments

Comments
 (0)