C++ find question

I

indrawati.yahya

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(),
and probably we can do away with find_if(). Like most things in C++, I
know there must be a good reason for this, I just cannot figure out
what that is at the moment. Thank you.
 
S

sonison.james

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(),
and probably we can do away with find_if(). Like most things in C++, I
know there must be a good reason for this, I just cannot figure out
what that is at the moment. Thank you.

The type of the object that the find algorithm compares should be one
that can be compared by using the operator ==

In case you want to use find algo for an object of your class you
could overload the operator == for that class.

Thanks and regards
SJ
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
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(),
and probably we can do away with find_if(). Like most things in C++, I
know there must be a good reason for this, I just cannot figure out
what that is at the moment. Thank you.

It must be a semantic reason.
In the case of find the semantic is an equivalence relation.

If you wanted to use another operator (which must be an inequality
operator), it would in fact become a predicate (i.e. "returns true if
the condition is satisfied, false if it is not") which means find_if
applies.

Michael
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
The type of the object that the find algorithm compares should be one
that can be compared by using the operator ==

In case you want to use find algo for an object of your class you
could overload the operator == for that class.

If the values are of the same type (or can be cast on the same type),
the following is more straightforward:
find_if(v.begin(),v.end(),bind2nd(less<type>(),value));

Michael
 
B

Bo Persson

(e-mail address removed) wrote:
:: 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(), and probably we can do away with find_if(). Like most
:: things in C++, I know there must be a good reason for this, I just
:: cannot figure out what that is at the moment. Thank you.

The different sort() templates have different number of parameters, so
the compiler can tell them apart.

A find() with a value and a find() with a functor would have the same
number of parameters, so the compiler cannot see which one you want to
call. Thus another name - find_if.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top