Friday 15 January 2010

c++ - Is there something like a "default comparator"? -


I've wraps a stud :: vector wrote a template made sure you always sort:

  template & lt; Typename T & gt; Class sorted vector {public: sorted vector (bool (* comparator) (t, t) = default Compaperetr & lt; T & gt;) {it-> comparator = comparator; } Insert zero value (t Nyuelement) {vect.insert (std :: low_bound (vect.begin ()), vect.end (), New Element, comparator), new Element); } Private: std :: vector & lt; T & gt; Vect; Bull (* comparator) (T, T); };  

I want to be able to use custom comparisons, but in most cases it will only T 's & lt; Operator However, I did not find a better way to use it

  template & lt; Typename T & gt; Bull default compiler (TA, TB) {as the basic parameter & lt; Return b < Is not a good way to do it?  

I can not use C ++ 11

There is no standard function template for what you want to do; But the standard function class template is std :: less . If you are to use the normal function objects instead of restricting yourself to a function pointer, the default is that it is easy to specify:

  template & lt; Typename T, typename Comparator = std:: less than & lt; T & gt; & Gt; Square sorted vector {public: sort vector (comparative comparator = comparative ()) {it-> comparator = comparator; } Zero Enter Value (T NewAlment) {// lower_bound accepts an appropriate function object, so no changes are required. (Vector.edit) Comparator), newElement); } Private: std :: vector & lt; T & gt; Vect; Comparative comparator; };  

No comments:

Post a Comment