Mem_fun_ref, mem_fun, not1, not2, ptr_fun

Source: Internet
Author: User

During program debugging today, we encountered such a problem: bool check (int elem); vecot <int> v ;... pos = find_if (v. begin (), v. end (), not1 (check) has an error. After searching for the information, you can find that the original reasons are 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 include 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. Www.2cto. comnot1 is not the only component with requirements in STL. The four standard function adapters (not1, not2, bind1st, and bind2nd) all need some structures provided by some typedef c ++ standard libraries: [cpp] 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 limit; typedef Arg2 limit; typedef Result result_type;}; not2 is also the same principle. not2 is used for a request containing two parameters. The function [cpp] pos = find_if (v. begin (), v. end (), not1 (ptr_fun (check) whose return value is bool is correct. [Cpp] # include <iostream> # include <string> # include <vector> # include <functional> # include <algorithm> using namespace std; bool check (int) {if (a % 2 = 0) {return true;} else {return false;} int main () {int a [] = {1, 2, 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 [cpp] class Person {public: void print () const {...} designed for member functions {...}} int main () {vector <Person> v ;... for_each (v. begin (), v. end (), men_fun_ref (& Person: print); // Number of called members} [cpp] 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? [cpp] # 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.