I 'm not understand this code and not run this code
please explain iterators ?
#include
#include
#include
using namespace std;
// Templates(templates gives u) + Iterators(Iterators gives you containers class) +Comparators (Different type of operation)
template
int search(T arr[],int n,T key){
for(int p=0;p<n;p++){
if(arr[p]==key){
return p;
}
}
return n;
}
// Templates + Iteratos
template<class ForwardIterator,class Compare T>
ForwardIterator search(ForwardIterator start ,ForwardIterator end ,T key, Compare cmp){
while(start!=end){
if(cmp(*start,key)){
return start;
}
start++;
}
return end;
}
class Book{
public:
string name;
int price;
Book()
{
}
Book(string name,int price){
this ->name = name;
this ->price = price;
}
};
class BookCompare{
public:
bool operator()(Book A,Book B){
if(A.name==B.name){
return true;
}
return false;
}
};
int main(){
Book b1("C++",100);//old edition
Book b2("python",120);
Book b3("java",130);
vector<Book>l;
l.push_back(b1);
l.push_back(b2);
l.push_back(b3);
Book booktofind("C++",110);//new edition
BookCompare cmp;
auto it = search(l.begin(),l.end(),booktofind,cmp);
if(it!=l.end()){
cout << "Book found in the library!";
}
else{
cout << "Not Found";
}
return 0;
}
/*
if(cmp(b1,booktofind)){
cout<<"True same books!";
}
*/
/*
Generic programming deals with compile-time mistakes, which are easier and faster to solve.
It maximizes the efficiency of the program. Generic programming allows
programmers to create their interface, methods, classes, and delegates. It also prioritizes type safety.
-> Forward Iterator :
Forward Iterator is a combination of Bidirectional and Random Access iterator.
Therefore, we can say that the forward iterator can be used to read and write to a container.
*/