Search code examples
c++structuremultiset

Multiset With Structures


I want to declare a multiset of a structure. The current form of my declaration of multiset is

struct obj
{
   //code
};

struct compare
{
    inline bool operator()(const obj &a, const obj &b)
    {
    //code
    }
};
multiset<obj,compare> mst;

Is there any other form of declaration which may use only one structre by overloading operator for obj.


Solution

  • Yes, you can overload operator < for your struct, either as a member function or as a free-standing one. This will make code more concise (in my purely subjective opinion), but in practice would not lead to any difference in performance.

    As a matter of fact, optimizing compiler is more than likely to generate exactly the same code (provided the actual comparison code is the same).