Find algorithm in STL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vermarajeev
    New Member
    • Aug 2006
    • 180

    Find algorithm in STL

    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

    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;
    }
    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
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by vermarajeev
    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

    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;
    }
    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

    Maybe:
    Your vector contains elements of type "myClass*", your operator== however compares elements of type "myClass". So, I would guess pointers to objects and not objects themselves are compared here. Hence, your operator is not called.

    Comment

    • zahidkhan
      New Member
      • Sep 2006
      • 22

      #3
      Originally posted by vermarajeev
      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

      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;
      }
      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
      Here internally algorithm find is comparing two pointers
      Since pointer of your class can't call the == operator
      you have to pass the object

      And you should use const myClass& as a parameter of == operator
      because algorithm find is receiving your object using const & and

      and there is no operator defined which will take your operands as const reference

      So the final solution is like this


      #include<iostre am>
      #include<vector >
      #include<algori thm>
      using namespace std;

      class myClass
      {
      public:
      myClass(){}
      ~myClass(){}
      friend bool operator==(cons t myClass& a, const myClass& b); // Modified
      };
      typedef std::vector<myC lass*> myClassList;
      typedef std::vector<myC lass>::iterator myClassListIter ator; // Modified

      bool operator==(cons t myClass& a, const myClass& b) //this doesnt get called // Modified
      {
      return true;
      }

      int main()
      {
      myClass* a = new myClass();
      myClass* b = new myClass();

      myClassList list;
      list.push_back( a);
      list.push_back( b);
      myClassListIter ator it = find(*list.begi n(), *list.end(), *a); //should call operator== // Modified
      if( it == *list.end() ) // Modified
      cout<<"Not found"<<std::en dl;
      else
      cout<<"Found"<< std::endl;
      return 0;
      }

      Comment

      • vermarajeev
        New Member
        • Aug 2006
        • 180

        #4
        Hi thankx,
        The solution works but I'm not still clear.

        Now first thing first, what is the main difference between
        Code:
        myClassListIterator it = find(list.begin(), list.end(), a);
        and
        Code:
        myClassListIterator it = find(*list.begin(), *list.end(), *a);
        How does the find algorithm interpret that.

        My understanding is
        I understand that a(in above code) is a pointer and *a(in above code) is an address. Now operator==(cons t myClass& a, const myClass& b) has address in it. So when I use *list.begin(), *list.end() in the above code, their addresses gets passed because of which operator== gets called. Did I make sense?

        If not please help me to correct myself.
        Thankx

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          *list.begin() is the contents of the first element in the list.
          *list.end() is the contents of the last element in the list.

          list.end() and list.begin() are pointers, references, and the * is deference. It cancels it out, leaving the original contents, rather than it's address.

          Comment

          • dtimes6
            New Member
            • Oct 2006
            • 73

            #6
            Try this:

            bool operator==(myCl ass* a, myClass* b)
            {
            return true;
            }

            Comment

            • vermarajeev
              New Member
              • Aug 2006
              • 180

              #7
              Originally posted by dtimes6
              Try this:

              bool operator==(myCl ass* a, myClass* b)
              {
              return true;
              }
              When I do that as you have suggested I get the following errors
              Code:
              : error C2803: 'operator ==' must have at least one formal parameter of class type
              : error C2803: 'operator ==' must have at least one formal parameter of class type
              Can you help me now....

              Thankx

              Comment

              • dtimes6
                New Member
                • Oct 2006
                • 73

                #8
                This is an compiler error that it can not compare two pointers.

                Use an obj to wrap your pointer, like this.

                #include<iostre am>
                #include<vector >
                #include<algori thm>
                using namespace std;

                class myClass
                {
                public:
                myClass(){}
                ~myClass(){}
                };

                class myClassPtr{
                public:
                myClass* p;
                myClassPtr( myClass* ptr): p(ptr) {}
                };

                typedef std::vector<myC lassPtr> myClassList;
                typedef std::vector<myC lassPtr>::itera tor myClassListIter ator;

                bool operator==(cons t myClassPtr& a, const myClassPtr& 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);
                myClassListIter ator it = find(list.begin (), list.end(), a); //should call operator==
                if( it == list.end() )
                cout<<"Not found"<<endl;
                else
                cout<<"Found"<< endl;
                return 0;
                }

                Comment

                • vermarajeev
                  New Member
                  • Aug 2006
                  • 180

                  #9
                  Hi, thankx for your reply...
                  It works fine but can you please explain me why we need the wrapper class myClassPtr and how that it is working....I'm unable to understrand that...

                  Waiting for your reply....

                  Comment

                  • dtimes6
                    New Member
                    • Oct 2006
                    • 73

                    #10
                    The pointer's operator== is a build-in
                    like bool operator==(int, int)

                    Comment

                    • D_C
                      Contributor
                      • Jun 2006
                      • 293

                      #11
                      Originally posted by dtimes6
                      Try this:

                      bool operator==(myCl ass* a, myClass* b)
                      {
                      return true;
                      }
                      I was looking at someone redefine greater than, or less than, some operator like that, and I think the comparison was between this and *rhs (right hand side). You may just have one parameter, and use "this" to refer to the one on the left hand side.

                      That way
                      Code:
                      (a == b)
                      calls
                      Code:
                      a.operator==(&b)

                      Comment

                      Working...