Solved

question about using bind2nd to convert a binary predicate to unary predicate

Posted on 2003-10-29
5
517 Views
Last Modified: 2012-05-04
In STL, "bind2nd" is used to convert a binary prediate to unary predicate. I try to declare a unary predicate by bind2nd, but it does not work.
Please tell me the reason.


#include<functional>
#include<algorithm>
#include<vector>

using namespace std;
int main()
{
      vector<int> v;
      typedef vector<int>::iterator iterator;

      v.push_back(1);      
      v.push_back(2);      
      v.push_back(3);      

      // this part work well
      iterator it1 = find_if(v.begin(), v.end(), bind2nd(equal_to<int>(), 3));

      // however, the following doesn't work
      unary_function<int,bool> p = bind2nd(equal_to<int>(), 3);      
      iterator it2 = find_if(v.begin(), v.end(), p);//when compile: error C2064: term does not evaluate to a function

      return 0;
}
0
Comment
Question by:zma64
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
PhilipNicoletti earned 125 total points
ID: 9643963
    binder2nd<equal_to<int> > p = bind2nd(equal_to<int>(),3);

Not really sure why. I saw this on the web some time ago and just
put it in my notes.

This also works:

     binder1st<equal_to<int> > p = bind1st(equal_to<int>(),3);
0
 
LVL 15

Expert Comment

by:efn
ID: 9646657
Good answer from PhilipNicoletti.  Here's why it works.

find_if wants a function object as its third parameter.  A function object is an object of a class that has an operator().  unary_function is just a structure with a couple of typedefs.  It doesn't have an operator() itself.  It's intended as a base class for function objects so they can inherit the typedefs.  So the compiler complained because it couldn't call p(), because p didn't have an operator ().

What you want as the type of p is something this is derived from unary_function, but has an operator ().  That's exactly what binder2nd is, and conveniently enough, a binder2nd is what bind2nd returns.  The binder2nd has a function object and a second parameter as members, and its operator() calls the function object.

--efn
0
 
LVL 4

Expert Comment

by:PhilipNicoletti
ID: 9646827
That makes sense,

when I tried the original code on Intel's cpp compiler, it gave
an error message about needing operator () or function
pointer (I'm not sure of the exact wording - I don't have
access to the compiler right now).
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546298
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: PhilipNicoletti {http:#9643963}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now