Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 75
IdTCPClient1->Disconnect(); not working 3 76
c++ getting the first 10 characters of a char* string 11 98
Dialogbox API leak? 18 98
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

791 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