Hi guys,
I have problem regarding find algorithm used in STL
Defination directly from book
The find algorithm is an operation (function) that can be applied to many STL containers. It searches a subrange of the elements in a container (or all the elements), looking for an element that is "equal to" a specified value; the equality operator (==) must be defined for the type of the container's elements.
In the defination above i'm having problem with the bold text
Now here is myClass as follows
But the problem is I'm unable to call the operator== defined in myClass. What is that I'm doing wrong in the above code????
Any comment will be highly appreciated
I have problem regarding find algorithm used in STL
Defination directly from book
The find algorithm is an operation (function) that can be applied to many STL containers. It searches a subrange of the elements in a container (or all the elements), looking for an element that is "equal to" a specified value; the equality operator (==) must be defined for the type of the container's elements.
In the defination above i'm having problem with the bold text
Now here is myClass as follows
Code:
#include<iostream> #include<vector> #include<algorithm> using namespace std; class myClass { public: myClass(){} ~myClass(){} friend bool operator==(myClass& a, myClass& b); }; typedef std::vector<myClass*> myClassList; typedef std::vector<myClass*>::iterator myClassListIterator; bool operator==(myClass& a, myClass& b) //this doesnt get called { return true; } int main() { myClass* a = new myClass(); myClass* b = new myClass(); myClassList list; list.push_back(a); list.push_back(b); myClassListIterator it = find(list.begin(), list.end(), a); //should call operator== if( it == list.end() ) cout<<"Not found"<<endl; else cout<<"Found"<<endl; return 0; }
Any comment will be highly appreciated
Comment