Friday 15 August 2014

c++ - Using mem_fun_ref if member function requires multiple parameters -


Unfortunately, I can not use C ++ 11 or Boost.

I have some code like the following

  struct cell {cell value}: value (value) {} int value; Bull Comparison Value (Cost Cell and Other) Constance {Return- It & gt; Value & lt; Other.value; }}; Int main () {const int size = 10; Of vector & lt; Cell & gt; Cells; For (int i = 0; i & lt; size; i ++) {cells.push_back (cell (rand ()); Cout & lt; & Lt; "[" & Lt; & Lt; Cells.back () Value & lt; & Lt; "]"; } Cout & lt; & Lt; "\ N \ n"; Int maximality = Maximum_Element (cells), Cells.end (), mem_fun_ref (& cell :: Compare value)) - & gt; Values; Int minvalue = min_element (cells.begin (), cells.end (), mem_fun_ref (& Cell :: CompareValue)) - & gt; Values; Cout & lt; & Lt; "Max =" & lt; & Lt; Maximum value & lt; & Lt; "\ N" & lt; & Lt; "Min =" & lt; & Lt; Minervalue & lt; & Lt; "\ N"; }   

But now I need to compare the function to compare the comparison of MODE.

  Compare bool (constellation and other , MODE_T mode) const {switch (mode) {...}}   

However, how can I update the use of max_element to work with this new function? The parameter will be similar to each part of the comparison. I tried to use many binds and accessories, but there is no benefit. Any help appreciated.

You can write functor , it will do that. Straight Comparison: Selvthmoda: Public Statt :: binary_function & lt; Cell, cell, boolean & gt; {PUBLIC: result_type comparison cellhelldmod (call first_guarg_type & amp; f; cons second_gram_type & amp; s) {return f. Compreable Value (S, Mode); Private: MODE_T mode; }; MODE_T mode; Int maxvalue = max_element (cells.begin (), cells.end (), compare wiki mode (mode)) - & gt; Values;

or std :: tr1 :: bind

  with MODE_T mode; Int maxvalue = max_element (cells.begin (), cells.end (), std :: tr1 :: dam (and cell :: compare price, _1, mode)) - & gt; Values;    

No comments:

Post a Comment