473,480 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ find question

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.

Aug 31 '07 #1
4 1833
On Aug 31, 12:33 pm, indrawati.ya...@gmail.com 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 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

Aug 31 '07 #2
in*************@gmail.com 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
Aug 31 '07 #3
so***********@gmail.com a écrit :
On Aug 31, 12:33 pm, indrawati.ya...@gmail.com 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 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>(),val ue));

Michael
Aug 31 '07 #4
in*************@gmail.com 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
Aug 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2182
by: Praveen | last post by:
In the ISPF editor I am using, for a particullar PO Dataset I am getting the result of FIND statement narrowed down to the colums 48-56. i.e. if I give "FIND 'TO' ALL", the result I am getting as...
5
2556
by: Jim Holder | last post by:
I tried building libpcap and tcpdump from my Red Hat 7 RPMs. The tcpdump make couldn't find ioccom.h and sockio.h. When a header file is missing, how do you find it? Thanks.
3
1364
by: Busin | last post by:
bool arr; arr = true; For the element arr, is there any fastest algorithm to fast find the two elements on both side of "n" and both elements are cloest to "23"? Thanks!
9
2105
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. For a...
7
1353
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
Hi, How can I implement regex to find complete or partial words or a group of words. Similar to the "Find" in MS Word. I need to scan text files eg look for "test" or "this is a test" or...
26
350
by: ak | last post by:
Recently at an interview i was asked the following question : Assuming the function lookupName is defined, what's wrong with this code (hint: 2 bugs)? const char *getName(const char *c) {...
2
5055
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
68094
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
3
6100
by: raylopez99 | last post by:
I suspect the answer to this question is that it's impossible, but how do I make the below code work, at the point where it breaks (marked below). See error CS0411 This is the complete code. ...
7
5333
by: Christian Meier | last post by:
Hello Newsgroup I have a question about the find function of std::set. When I have a "std::set<int*>", why can't I call the find() function with an "const int*"? I know that my key type is...
0
7041
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
6908
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
7043
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,...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.