-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path04_comparator_class.cpp
More file actions
175 lines (145 loc) · 3.75 KB
/
04_comparator_class.cpp
File metadata and controls
175 lines (145 loc) · 3.75 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
TOPIC: Comparator Class
What we have learned till now:
- If we want to make generic funtion we should use "templates".
(because with the help of templates we get freedom from data types.)
- we can also use "templates + Iterators" as with the help of iterators we get freedom from data structure.)
Comparator
- Comparators gives us freedom from different types of operations, operating on the data.
Eg: If b1 & b2 are 2 books (i.e object).
Then,
if b1 < b2, then we can define our own comparison.
OR
if b1 == b2, then we can define our own comparison.
... ...
Now, with the help of "comparator" we can make our code more generic.
*/
#include<iostream>
#include<list>
#include<vector>
using namespace std;
// [Templates] 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;
}
/*
Steps:
- define search function with return type "ForwardIterator"
- this search function will accept 3 things: starting point & ending point of that conatiner & the element
we want to search.
- we have to make it as a Template function<class A, classB> with 2 class: [One for container & Second for datatype]
- Now, write logic for the function.
*/
// [Templates + Iterators] Linear Search
template<class ForwardIterator, class T>
ForwardIterator search(ForwardIterator start, ForwardIterator end, T key)
{
while(start != end)
{
if(*start == key)
{
return start;
}
start++;
}
// return end, if element not found
return end;
}
// [Templates + Iterators + Comparators] Linear Search
template<class ForwardIterator, class T, class Compare>
ForwardIterator search(ForwardIterator start, ForwardIterator end, T key, Compare cmp)
{
while(start != end)
{
if(cmp(*start,key))
{
return start;
}
start++;
}
// return end, if element not found
return end;
}
class Book
{
public:
string name;
int price;
Book()
{
//
}
Book(string name,int price)
{
this->name = name;
this->price = price;
}
};
// Comparator Class
class BookCompare
{
public:
bool operator()(Book A, Book B) // overloaded () operator
{
if(A.name == B.name)
{
return true;
}
return false;
}
};
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";
*/
Book b1("C++", 100); // old book edition
Book b2("Python", 120);
Book b3("Java", 130);
Book b4(b1);
vector<Book> l;
// list<Book> l;
l.push_back(b1);
l.push_back(b2);
l.push_back(b3);
// Search key (of datatype Book object)
Book key("C++", 110); // new book edition
BookCompare cmp; // "cmp" is an object of "BookCompare" class
/*
if(cmp(b1,key))
{
cout << "True: same books!";
}
*/
auto it = search(l.begin(), l.end(), key, cmp);
// list<Book>::iterator it = search(l.begin(), l.end(), key, cmp);
// vector<Book>::iterator it = search(l.begin(), l.end(), key, cmp);
if(it == l.end())
{
cout << "Book Not Found" << endl;
}
else{
cout << "Book found in the library" << endl;
}
return 0;
}
/*
OUTPUT:
when key = ("C++", 110)
OUTPUT: Book found in the library
when key = ("C", 110)
OUTPUT: Book NOT found
*/