Connecting Tech Pros Worldwide Help | Site Map

Find algorithm in STL

Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#1: Oct 11 '06
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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. class myClass
  7. {
  8. public:
  9.     myClass(){}
  10.     ~myClass(){}
  11.     friend bool operator==(myClass& a, myClass& b);
  12. };
  13. typedef std::vector<myClass*>            myClassList;
  14. typedef std::vector<myClass*>::iterator   myClassListIterator;
  15.  
  16. bool operator==(myClass& a, myClass& b) //this doesnt get called
  17. {
  18.     return true;
  19. }
  20.  
  21. int main()
  22. {
  23.     myClass* a = new myClass();
  24.     myClass* b = new myClass();
  25.  
  26.     myClassList list;
  27.     list.push_back(a);
  28.     list.push_back(b);
  29.     myClassListIterator it = find(list.begin(), list.end(), a); //should call operator==
  30.     if( it == list.end() )
  31.         cout<<"Not found"<<endl;
  32.     else
  33.         cout<<"Found"<<endl;
  34.     return 0;
  35. }
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's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#2: Oct 11 '06

re: Find algorithm in STL


Quote:

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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. class myClass
  7. {
  8. public:
  9.     myClass(){}
  10.     ~myClass(){}
  11.     friend bool operator==(myClass& a, myClass& b);
  12. };
  13. typedef std::vector<myClass*>            myClassList;
  14. typedef std::vector<myClass*>::iterator   myClassListIterator;
  15.  
  16. bool operator==(myClass& a, myClass& b) //this doesnt get called
  17. {
  18.     return true;
  19. }
  20.  
  21. int main()
  22. {
  23.     myClass* a = new myClass();
  24.     myClass* b = new myClass();
  25.  
  26.     myClassList list;
  27.     list.push_back(a);
  28.     list.push_back(b);
  29.     myClassListIterator it = find(list.begin(), list.end(), a); //should call operator==
  30.     if( it == list.end() )
  31.         cout<<"Not found"<<endl;
  32.     else
  33.         cout<<"Found"<<endl;
  34.     return 0;
  35. }
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.
Newbie
 
Join Date: Sep 2006
Posts: 22
#3: Oct 11 '06

re: Find algorithm in STL


Quote:

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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. class myClass
  7. {
  8. public:
  9.     myClass(){}
  10.     ~myClass(){}
  11.     friend bool operator==(myClass& a, myClass& b);
  12. };
  13. typedef std::vector<myClass*>            myClassList;
  14. typedef std::vector<myClass*>::iterator   myClassListIterator;
  15.  
  16. bool operator==(myClass& a, myClass& b) //this doesnt get called
  17. {
  18.     return true;
  19. }
  20.  
  21. int main()
  22. {
  23.     myClass* a = new myClass();
  24.     myClass* b = new myClass();
  25.  
  26.     myClassList list;
  27.     list.push_back(a);
  28.     list.push_back(b);
  29.     myClassListIterator it = find(list.begin(), list.end(), a); //should call operator==
  30.     if( it == list.end() )
  31.         cout<<"Not found"<<endl;
  32.     else
  33.         cout<<"Found"<<endl;
  34.     return 0;
  35. }
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<iostream>
#include<vector>
#include<algorithm>
using namespace std;

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

bool operator==(const 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);
myClassListIterator it = find(*list.begin(), *list.end(), *a); //should call operator== // Modified
if( it == *list.end() ) // Modified
cout<<"Not found"<<std::endl;
else
cout<<"Found"<<std::endl;
return 0;
}
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#4: Oct 12 '06

re: Find algorithm in STL


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

Now first thing first, what is the main difference between
Expand|Select|Wrap|Line Numbers
  1. myClassListIterator it = find(list.begin(), list.end(), a); 
and
Expand|Select|Wrap|Line Numbers
  1. 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==(const 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
D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#5: Oct 13 '06

re: Find algorithm in STL


*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.
Member
 
Join Date: Oct 2006
Posts: 73
#6: Oct 13 '06

re: Find algorithm in STL


Try this:

bool operator==(myClass* a, myClass* b)
{
return true;
}
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#7: Oct 13 '06

re: Find algorithm in STL


Quote:

Originally Posted by dtimes6

Try this:

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

When I do that as you have suggested I get the following errors
Expand|Select|Wrap|Line Numbers
  1. : error C2803: 'operator ==' must have at least one formal parameter of class type
  2. : error C2803: 'operator ==' must have at least one formal parameter of class type
Can you help me now....

Thankx
Member
 
Join Date: Oct 2006
Posts: 73
#8: Oct 13 '06

re: Find algorithm in STL


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

Use an obj to wrap your pointer, like this.

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

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

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

typedef std::vector<myClassPtr> myClassList;
typedef std::vector<myClassPtr>::iterator myClassListIterator;

bool operator==(const 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);
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;
}
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#9: Oct 13 '06

re: Find algorithm in STL


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....
Member
 
Join Date: Oct 2006
Posts: 73
#10: Oct 13 '06

re: Find algorithm in STL


The pointer's operator== is a build-in
like bool operator==(int, int)
D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#11: Oct 13 '06

re: Find algorithm in STL


Quote:

Originally Posted by dtimes6

Try this:

bool operator==(myClass* 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
Expand|Select|Wrap|Line Numbers
  1. (a == b)
calls
Expand|Select|Wrap|Line Numbers
  1. a.operator==(&b)
Reply