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 - #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
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 - #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.
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 - #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<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;
}
Hi thankx,
The solution works but I'm not still clear.
Now first thing first, what is the main difference between - myClassListIterator it = find(list.begin(), list.end(), a);
and - 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
*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.
Try this:
bool operator==(myClass* a, myClass* b)
{
return true;
}
Try this:
bool operator==(myClass* a, myClass* b)
{
return true;
}
When I do that as you have suggested I get the following errors - : 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
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;
}
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....
The pointer's operator== is a build-in
like bool operator==(int, int)
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
calls Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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 = ...;
|
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
|
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...
|
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(),...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |