Solved

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

Posted on 2003-10-29
5
521 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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