I tried the above concept of comparator class by my own for pair. Both of the codes meant same but by using pair, it is showing me TLE?. Can you please analyse?
#include
#include
using namespace std;
template<class forwardIterator, class T, class ob>
forwardIterator search(forwardIterator start, forwardIterator end, T key, ob cmp){
while(start!=end){
if(cmp(*start,key))
return start;
}
return end;
}
class comparator{
public:
bool operator()(pair<int,string> p1, pair<int,string> p2){
if(p1.second==p2.second){
return true;
}
return false;
}
};
int main(){
vector<pair<int,string>> v;
v.push_back(make_pair(100,“c++”));
v.push_back(make_pair(1000,“java”));
v.push_back(make_pair(120,“python”));
pair<int,string> booktofind;
booktofind.first=105;
booktofind.second=“java”;
comparator cmp;
auto it = search(v.begin(),v.end(), booktofind, cmp);
if(it!=v.end()){
cout<<"book found!";
}
else
cout<<"book not found!";
return 0;
}