-
Notifications
You must be signed in to change notification settings - Fork 10
/
01_generic_programming_with_templates.cpp
80 lines (59 loc) · 1.97 KB
/
01_generic_programming_with_templates.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
TOPIC: Generic Programming with Templates
Basics:
- The major advantage of C++ Standard Template Library(STL) is "all the containers" & "all the algorithms"
provided by STL is generic.
- Generic means they can work with different data types.
Eg: if you want to sort array of intergers, you can use the sort function.
if you want to sort array of books (objects), then also you can use the sort function.
So, underline datatype doesn't matter.
- The only thing that it will need is, How do you compare 2 book object ?
For this, you have to define a comparison using a Comparator function (that will make the sort function
work for a array of books)
Generic Programming can be done a 2 levels:-
1. Algorithm
2. Container (data structures Eg: vector)
Let's make a Generic Function! (at Algorithm Level)
- the way to do is using Templates.
Eg: template <class T> Or
template <typename T>
*/
#include<iostream>
using namespace std;
// Linear Search
template<typename T>
int search(T arr[], int n, T key)
{
for(int i=0; i<n; i++)
{
if(arr[i] == key)
{
return i;
}
}
// return n, if element not found
return n;
}
int main()
{
int arr[] = {1,2,3,5,17,20}; // array of interger
int n = sizeof(arr)/sizeof(int);
int key = 17;
cout << "Index: " << search(arr, n, key) << "\n\n";
float b[] = {1.1, 1.6 , 2.3, 2.5}; // array of float
n = sizeof(b)/sizeof(int);
float fkey = 1.6;
cout << "Index: " << search(b, n, fkey) << "\n\n";
char c[] = {'a', 'd' , 'e', '\0'}; // array of char
n = sizeof(c)/sizeof(int);
char ckey = 'e';
cout << "Index: " << search(c, n, ckey) << endl;
// search(book, ....) // array of Objects
return 0;
}
/*
OUTPUT:
Index: 4
Index: 1
Index: 0
*/