Mem_fun_ref, mem_fun, not1, not2, ptr_fun

Source: Internet
Author: User

When I debugged the program today, I encountered such a problem.

Bool check (int elem );

Vecot <int> V ;...

Pos = find_if (V. Begin (), V. End (), not1 (check) has an error. After finding the information, you can find that the original cause is as follows:

The only thing ptr_fun does is to make some typedef valid (the type of the parameter included in the operator () of the function class and its return type. For binary_function, You need to specify three types: the type of the first and second parameters of your operator, and the type returned by your operator; the two base classes typedef are argument_type, first_argument_type, second_argument_type, and result_type.
). That's it. Not1 requires these typedef, Which is why not1 can be applied to ptr_fun, but not1 cannot be applied directly to check. Because it is a low-level function pointer, check lacks typedef required by not1.
Not1 is not the only component with requirements in STL. Four standard function adapters (not1, not2, bind1st, and bind2nd) require some typedef

The C ++ Standard Library provides the following structures:

template<typename Arg,typename Result>struct unary_function{    typedef Arg argument_type;    typedef Result result_type;};template<typename Arg1,typename Arg2,typename Result>struct binary_function{     typedef Arg1 first_argument_type;     typedef Arg2 second_argument_type;     typedef Result result_type;};

 

The same applies to not2. not2 is a function that contains two parameters and returns bool.

Pos = find_if (V. Begin (), V. End (), not1 (ptr_fun (check); this is correct.
#include<iostream>#include<string>#include<vector>#include<functional>#include<algorithm>using namespace std;bool check(int a){ if(a%2==0){  return true; }else{  return false; }}int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; vector<int>v(a,a+10); vector<int>::iterator it=find_if(v.begin(),v.end(),not1(ptr_fun(check))); cout<<*it<<endl; system("pause"); return 0;}

 

Mem_fun_ref and men_fun are function adapters designed for member functions.

Class person {public: void print () const {...}} int main () {vector <person> V ;... for_each (v. begin (), V. end (), men_fun_ref (& person: print); // Number of called members}

 

Class person {public: void print () const {...}} int main () {vector <person *> V ;... for_each (v. begin (), V. end (), men_fun (& person: print); // Number of called members}

Do you know the difference?

#include<iostream>#include<vector>#include<algorithm>#include<functional>#include<cmath>using namespace std;template<typename T1,typename T2>struct fopow:public binary_function<T1,T2,T1>{T1 operator()(T1 base,T2 exp) const{return pow(base,exp);}};int main(){vector<int>v;for(int i=1;i<=9;++i){v.push_back(i);}transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),bind1st(fopow<float,int>(),3));cout<<endl;transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),bind2nd(fopow<float,int>(),3));cout<<endl;system("pause");return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.