Wednesday 15 May 2013

Comparison function for C++ STL set: Can comparison function be a member function of a class? -


I have to use the STL set and I want to define my own comparison function. But according to my requirement this comparison function should not be a global, rather it should be a public member of a class.

  // class definition class identifier {public: identifier (); ~ Identifier (); Child Comparison Identifier (Intident Identifier, Int Identifier); Public: std :: set & lt; Identifier, bool (*) (identifier, identifier) ​​& gt; Set_instance; }; // Class Constructor Identifier :: Identifier () {std :: set & lt; Identifier, boole (*) (identifier, identifier) ​​& gt; Set_instance (compare_identifier); }   

If I write a piece as mentioned above, it does not compile as a comparison. The prototype of the function does not match the comparison_findler () function.

Is there any way to do this?

A non-static member job this therefore your comparison_increator There are actually three parameters if you need this non-static member function, you need to force the first parameter in the member function for an example of IDENTIFIERS , for example For,

  Include # lt; Set & gt; #include & lt; Functional & gt; Structure identifier {int id; }; Class identifier {public: identifier (): set_instance (std :: bind (& identifier :: comparison_ identifier, this, std :: placeholder :: _ 1, std :: placeholders :: _ 2}) {} bool comparison_identifier (Constident Identifier & LHS, Constant Identifier & amp; rhs) {Return lhs.id & lt; Rhs.id; } Public: std :: set & lt; Identifier, std :: function & lt; Bool (constnant & amp; const identifier & amp;; gt; set_instance;};    

No comments:

Post a Comment