468,462 Members | 1,811 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,462 developers. It's quick & easy.

Find algorithm in STL

180 100+
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
Oct 11 '06 #1
10 21240
arne
315 Expert 100+
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.
Oct 11 '06 #2
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;
}
Oct 11 '06 #3
vermarajeev
180 100+
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
Oct 12 '06 #4
D_C
293 100+
*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.
Oct 13 '06 #5
dtimes6
73
Try this:

bool operator==(myClass* a, myClass* b)
{
return true;
}
Oct 13 '06 #6
vermarajeev
180 100+
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
Oct 13 '06 #7
dtimes6
73
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;
}
Oct 13 '06 #8
vermarajeev
180 100+
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....
Oct 13 '06 #9
dtimes6
73
The pointer's operator== is a build-in
like bool operator==(int, int)
Oct 13 '06 #10
D_C
293 100+
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)
Oct 13 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by cody | last post: by
7 posts views Thread by Bit byte | last post: by
5 posts views Thread by Draw | last post: by
10 posts views Thread by Christian Chrismann | last post: by
1 post views Thread by vermarajeev | last post: by
prometheuzz
4 posts views Thread by prometheuzz | last post: by
4 posts views Thread by indrawati.yahya | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.