Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-10-29
5
Medium Priority
?
530 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 375 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

604 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