STL C + + Std::bind Operation example, copy function operation matching algorithm library operation

Source: Internet
Author: User

1, Stl::bind and Std::mem_fun_ref series with the use of problems, multi-Parameter form does not know how to organize. The operation of the adapter is really uncomfortable!!! Can only be used in a daze. Only lambda or transfer operations are considered when using non-qualitative algorithms when used in containers. Cond


Functor-adapter_p431.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <algorithm>//element operation algorithm # include <functional>//function adapter, bind, compose, negate# Include <iostream> #include <iterator> #include <vector> #include <memory>using std::cout; Using std::cin;using std::endl;bool print (int i) {std::cout<< i << ""; return true;} class int{public:explicit int (int i): m_i (i) {};~int (void) {};void print1 (void) const{cout<< "[" <<m_i< < "]";} void Print2 (Int &i) const{cout<< "[" <<i.m_i<< "]";} static void Print3 (Int &i) {cout<< "[" <<i.m_i<< "]";} int M_i;private:};int operator + (const int &lhi,const int &AMP;RHI) {int temp (lhi.m_i); Temp.m_i+=rhi.m_i;return Temp ;}    typedef BOOL (*FUNPTR) (int), typedef std::function<void () > funobejct;      Class A {public:virtual void F () {std::cout<< "a::f ()" <<endl; } void Init () {//std::bind can exhibit polymorphic behavior funobEjct F=std::bind (&a::f,this);      f (); } bool Print (int i) {std::cout<< i << ":"; return true;}  inline bool operator () (int i) {std::cout<< i << "/"; return true;}};      Class B:public A {public:virtual void F () {cout<< "b::f ()" <<endl;  }  }; VOID Inc (int &a) {++a;} int _tmain (int argc, _tchar* argv[]) {std::ostream_iterator<int> out_it (cout, ",");//The output iterator is bound to the command-window output line, and each output is populated with one, Distinguish from print int ia[6] = {2,21,12,7,19,23};STD::SHARED_PTR&LT;A&GT;PA (new B ()); The parent class pointer points to the subclass object Pa->init (); Multiple-state, function-bound implementations of multiple/** template<class _ret,class _rx,class _farg0,class _arg0> inlinetypename enable_if<!is_same& Lt;_ret, _rx>::value,_bind<true, _ret, _pmd_wrap<_rx _farg0::*, _rx, _farg0>, _arg0>>::typebind (_Rx _FARG0::* Const _PMD, _arg0&& _a0) {//Bind a wrapped member object Pointerreturn (_bind<true, _ret,_pmd_wrap&lt ; _rx _farg0::*, _rx, _farg0>, _arg0> (_pmd_wrap<_rx _farg0::*, _rx, _farG0> (_PMD), _std forward<_arg0> (_a0)));} /std::function<bool (int) > Printint = Std::bind (&b::p rint,pa,std::p laceholders::_1);//std::function <bool (int) > PrintInt2 = Std::bind (&a::p rint,pa,std::p laceholders::_1);p rintint (;p) RintInt2 (1002); typedef std::function<bool (int) > FP; FP T_FP = fp (print); t_fp cout<<endl;//find data not less than 12 std::vector<int> IV (IA,IA+6); Std::copy_if (Iv.begin (), Iv.end (), Out_it,std::not1 (std::bind2nd (std::less<int> (),));//cout<< std::count_if (ia,ia+6,std :: Not1 (Std::bind (f1,12))) <<endl;cout<<endl;int nums = std::count_if (Iv.begin (), Iv.end (), Std::bind (std ::logical_and<bool> (), Std::bind (std::less<int> (), std::p laceholders::_1,30), Std::bind (std::greater <int> (), std::p laceholders::_1,12)));cout<< "(12,30):" <<nums<<endl;//executes such a code inside the loop: _ Pred (*first). _pred is a Function object Std::bind (std::less<int> (), std::p laceholders::_1,12) produces an object int nums2= std::count_if (Iv.begin (), Iv.end (), StD::bind (std::less<int> (), std::p laceholders::_1,12));cout<< "(-,12):" <<nums2<<endl;// Use the iterator output so that the data std::copy (Iv.begin (), Iv.end (), out_it), cout<<endl;//uses the function name Output Std::for_each (Iv.begin (), Iv.end (), print);//The function operation in the class is saved in a certain location, I want to read other books to know. An imitation function object refers to a functor object, the normal function object is the function name, to be continued cout<<endl;//using the functor object output, need to pass the pointer processing, because the STL function library definition uses the template, does not carry on the type check (the IDE also cannot complete the member reminder, Because there is no code associated with the type type of the operator,//As long as there is a load () overload can be compiled. If you use the PA pass value, the internally called () operator, but the native pointer type does not perform a operator () operation, the error is compiled. Std::for_each (Iv.begin (), Iv.end (), (*PA));cout<<endl; Funptr FP1 = print; function pointers and function names are the same thing? FP1 cout<<endl;//uses the function pointer output Std::for_each (Iv.begin (), Iv.end (), FP1), or the function operation in the class is saved somewhere, and I have to read other books to know. An imitation function object refers to a functor object, the normal function object is the function name, to be continued cout<<endl;//modified the general function of the STL algorithm, this thing has not seen, not a little understand Std::for_each (Iv.begin (), Iv.end (), std::p tr_fun (FP1));//Use the packaged function to finally call the structure std::p tr_fun (*begin ()), which executes FP1 (*begin ()) cout<<endl;//#define _bind_ IMPLICIT1 (//template_list1, Padding_list1, LIST1, COMMA1,//template_list2, Padding_list2, LIST2, COMMA2)//template <class _rx//comma1 LIST1 (_class_type)//comma2 LIST2 (_class_typex) > inline//_bind<true, _RX, _RX (* const) (LIST1 (_type) ) COMMA2 LIST2 (_typex) >//bind (_rx (*_pfx) (LIST1 (_type)) COMMA2 LIST2 (_typex_refref_arg))//{/* bind a function pointe R *///return (_bind<true, _RX, _RX (* const) (LIST1 (_type))//comma2 LIST2 (_typex) > (_pfx COMMA2 LIST2 (_forward_argx ))); }//Note: COMMA: comma, the parameter used is _typex_refref_arg/*1: #define _TYPEX_REFREF (num) _var_typex (num) &&2: #define _forward _ARGX (num) _std Forward<_var_typex (num) > (_VAR_VALX (num)) _var_typex is an rvalue type, so the process of calling the jump again, Estimate what happened: N is converted to an rvalue type and then produces a copy, and the Positive solution:std::forward<t> (U) has two parameters: T and U. When T is an lvalue reference type, u will be converted to an lvalue of type T, otherwise u will be converted to the T-type right value. This definition of Std::forward is intended to solve the problem of perfect forwarding of parameters in a function template that uses rvalue reference parameters. */int n = 0;std::bind (inc,n);//The source uses print (n),//0;std::bind (Inc,std::ref (n)) ();p rint (n);//finally 1. Int T1 (3), T2 (7), T3 (+), T4 (+), T5 (;std::vector<int>) Vint2;vint2.push_back T1; COCOS2DX in the Testcpp has a segment reflection code, you can consider using up, short-term risk familiar with c++11 stlvint2.push_back (T2); Vint2.push_back (T3);Vint2.push_back (T4); Vint2.push_back (T5);//mem_fun is a qualitative change algorithm. When for_each//the difference between the two: the role and usage of//MEM_FUN_REF is the same as Mem_fun, the only difference is://When the container is stored in the object entity with the mem_fun_ref,//when the container is stored in the object's pointer to use Mem_fun. Error C3867: "Int::p rint1": The function call is missing a parameter list, use "&int::p rint1" To create a pointer to a member Std::for_each (Vint2.begin (), Vint2.end (), Std::mem_fun_ref (&int::p rint1));//If you use a pointer that is not directed to the function name, the error cout<<endl;; Std::for_each (Vint2.begin (), Vint2.end (), Std::bind (Int::p rint3,std::p laceholders::_1));//As long as the correctness of the function interface can be ensured, std:: The first argument of the bind constructor must be an object that provides operator (), which can be an imitation function and a function name, using static because the operation of the object is manipulated by the function//std::for_each (Vint2.begin (), Vint2.end (), Std::mem_fun_ref (&std::bind (Int::p rint3,std::p laceholders::_1)));cout<<endl;; Std::for_each (Vint2.begin (), Vint2.end (), Std::bind (std::p lus<int> (), std::p laceholders::_1,int (3))), std:: For_each (Vint2.begin (), Vint2.end (), Std::mem_fun_ref (&int::p rint1));//If you use a pointer that does not refer to the function name, you will get an error cout<<endl;// //std::for_each (Vint2.begin (), Vint2.end (), Std::bind (, std::p laceholders::_1,int (3)) cannot be implemented using STLMEM_FUN_REF for the time being ;//std::for_each(Vint2.begin (), Vint2.end (), Std::mem_fun_ref (&int::p rint1)), or//If you use a pointer that does not refer to the function name, the error//cout<<endl;return 0; 





STL C + + Std::bind Operation example, copy function operation matching algorithm library operation

Related Article

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.