Solved

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

Posted on 2003-10-29
5
524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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