forked from anishsingh20/Programming-in-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparatorclass example.cpp
More file actions
82 lines (71 loc) · 1.44 KB
/
Copy pathcomparatorclass example.cpp
File metadata and controls
82 lines (71 loc) · 1.44 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
// C++ program for the Comparator Class
// for implementing linear search
#include <bits/stdc++.h>
using namespace std;
// Generic function to search for object
template <class ForwardIterator, class T>
ForwardIterator search(
ForwardIterator start,
ForwardIterator end, T key)
{
// Iterate until start equals to end
while (start != end) {
// If the value with given key is
// found the return that index
if (*start == key) {
return start;
}
start++;
}
return end;
}
// Student Class
class student {
public:
// To store Name and Roll Number
string name;
int rollnum;
// Overloaded Constructor
student(string name, int rollnum)
{
this->name = name;
this->rollnum = rollnum;
}
};
// Comparator Class to compare 2 objects
class studentcompare {
public:
// Comparator function
bool operator()(student a,
student b)
{
// If values are the same then
// return true
if (a.name == b.name) {
return true;
}
return false;
}
};
// Driver Code
int main()
{
// Object of class student
student s1("Raj", 23);
student s2("Prerna", 24);
// List of students
list<student> s;
s.push_back(s1);
s.push_back(s2);
// Search student("Prerna", 24)
student searchstudent("Prerna", 24);
studentcompare cmp;
// Print if element is found
if (cmp(s2, searchstudent)) {
cout << "Student found!";
}
else {
cout << "Not found";
}
return 0;
}