c++ - How to"adapt" functors to use with map/multimap? -


I'm having difficulty using std :: for_each and other algorithms, and to know if any help I could develop a fun doctor who could pass the appropriate parameters for "normal" functions.

My typical problem with map / multimap is that working with the evaluators of the std :: pair (I mean, the mapped type), instead of their ethernet values, is required. So, my question is, is there any way to give proper value to the function designed to work with any one type of map / multimap type?

Here is my sample code:

  // main type for multimap typedef enum {...} module; // multimap struct patch as my map type {apply zero (bool enabled); } // I have some factories designed to work directly with patch pointers / because in other places I set & lt; Patch * & gt; Or apply other container structures on Conditions: Public Anari_function & lt; Patch *, blank & gt; {Zero operator () (patch * patch) {if (some_condition) apply patch-> (enable_or_not); }} // Somewhere I have some modules multimap & lt; Module, patch * & gt; Patches; // The problem is that // a `pair and lieutenant with a map or multimap results; For the argument, the patch *> is not as desired as the patch * module, patch * for_each (patches.begin (), patches.end (), applied oncondition (...));  

I think that maybe combining bind1st or bind2nd with combination with memdfun can solve this problem, or the way i can think is to create a new fun which is original The functor stores and passes the right member of the pair, but I'm not managing to get any good results. Can anyone give some advice with experience in STL?

  edit1  

OK, I can get the best I can without boost or using additional temporary container following:

< Pre> #include & lt; Functional & gt; # Include & lt; Utility & gt; using namespace std; //////////////////////////////////////////////// //////////////////////// // Any factor to be called should be taken from unary_function or //. The result_type and argument_type are defined // template 'first' should be set to pair: first_type template & lt; Typename first, typename Func & gt; Class passSecond_t: public unary_function & lt; Pair and lieutenant; First, type name FUNC :: argument_type>, typename Func :: result_type & gt; {Funk * Funk; Public: passSecond_t (Function and Fun): Funk (and Funny) {} result_type operator () (argument_type value) {return (* function) (value seconds); }}; // Construction Assistant, unfortunately the 'first' should be clearly specified in the template & lt; Typename first, typename Func & gt; PassSecond_t & lt; First, Funk & gt; Passsecond (Funk & Funny) {Return PassedCand_T < First, Funky & gt; (Functor); } // The following sample # is included & lt; Map & gt; # Include & lt; Algorithm & gt; # Include & lt; Iostream & gt; Straight sample class {zero performance (four * lesson) {cout & lt; & Lt; "This:" & lt; & Lt; This & lt; & Lt; ", Text:" & lt; & Lt; Text & lt; & Lt; Endl; }}; Structure Sample Factor: public unary_function & lt; SampleClass *, Zero & gt; {Four * lesson; Sample Factor (four * text_): text (text_) {} resulted in methodology () (argument_type argument) {argu- & gt; Execute (text); }}; Zero main () {maps & lt; Int, SampleClass * & gt; Mymap; Sample Class S1, S2; Mammoth [0] = & amp; S1; MimeP [1] = & amp; S2; Sample Factor MatchField ("My Text"); For_each (mymap.begin (), mymap.end (), passsecond & lt; int & gt; (myfunctor)); I have a boost solution initially:
  Code> for_each (patches.begin ()), patches.end (), boost :: bind (apply oncondition (...), boost :: bind (& map_type :: value_type :: second, _1)));  

This: the second member of the pair: takes it, and then apply it to the operator of the oncondition (). The type of map is map_type ( multimap & lt; modules, patch * & gt; exactly).


Comments