473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 21839
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

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

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
4
by: Code4u | last post by:
I need to write an algorithm that sheds the outliers in a large data set, for example, I might want to ignore the smallest 2% of values and find the next smallest. Boost has a nth_element...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
7
by: Bit byte | last post by:
I have the ff code: list<string> *m_alive_list ; list<string>::iterator my_iter; my_iter = find(m_alive_list->begin(), m_alive_list->end, string(inbox) ; Compiler barfs on 2nd line with this...
5
by: Draw | last post by:
Hi All, Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range...
10
by: Christian Chrismann | last post by:
Hi, I've a question on the STL find algorithm: My code: int main( void ) { vector< ClassA* myVector; ClassA *ptrElement1 = ...;
1
by: vermarajeev | last post by:
Hi, EveryBody This question is really interesting one. My question is related to "STL Find Algorithm" Defination Direct from book Now my questions are
4
prometheuzz
by: prometheuzz | last post by:
Hello (Java) enthusiasts, In this article I’d like to tell you a little bit about graphs and how you can search a graph using the BFS (breadth first search) algorithm. I’ll address, and...
4
by: indrawati.yahya | last post by:
Just out of curiosity, why doesn't C++ find() accept a binary predicate so users can do equality comparisons other than the == operator? In this case, the use of find() will be similar to sort(),...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.