How to use std :: multiset with several comparator functions?

Good afternoon, I have a C ++ Range class that implements operator < to use std::multiset<Range> ranges_type .

Since the multiset constructor does not specify an arbitrary comparator functor, it uses std::less operator < .

However, I need to use the second comparator functor for std::multiset ranges_type . In particular, I would indicate the second comparator: std::multiset<Range, PointerCompare> where the struct PointerCompare looks like this:

 struct PointerCompare{ bool operator()(const Range& a, const Range& b) const { return (a.mPtr == b.mPtr) } 

Is it possible to use std:multiset with several comparator functions or is there a workaround? thank you

The Range class is as follows:

 class Range { public: explicit Range(int item){ mLow = item; mHigh = item; mPtr = 0; } Range(int low, int high, char* ptr = 0,char* mapptr = 0){ mLow = low; mHigh = high; mPtr = ptr; } Range(void){ mLow = 0; mHigh = 0; mPtr = 0; } Range(const Range& r): mLow(r.mLow), mHigh(r.mHigh), mPtr(r.mPtr) { } bool operator==(const Range& rhs) const{ return (mLow <= rhs.mLow && mHigh >= rhs.mHigh); } bool operator<(const Range& rhs) const{ return mHigh < rhs.mHigh; } int low() const { return mLow; } int high() const { return mHigh; } char* getPtr() const { return mPtr; } private: int mLow; int mHigh; char* mPtr; }; // class Range 
+4
source share
2 answers

It sounds just as if you were using something from Boost :: MultiIndex instead of trying to force several different functions of the comp comparator stand :: multiset. They have a bunch of different types of containers ( see here .) In particular, I would look at ordered_indices .

+3
source

Perhaps I found a workaround for several comparator functions: Here:

Range targetRange = Range (PreviousNCopy, PreviousN, TmpPrevMapPtr);

bool Found = std :: binary_search (range_type.begin (), range_type.end (), targetRange, MyComparator ());

where: MyComparator is the structure: struct MyComparator {
bool operator () (const Range & d1, const Range & d2) const {
return d1.getPtr () <d2.getPtr ();
}};

std :: binary_search take o (log n), but std :: multiset range_type should always be sorted. Thanks.

+1
source

All Articles