Frage

I am trying to obtain a sorting of class objects according to two of its members in such a way: sort according to a member. If two objects have the same value (or if the difference is below some threshold) then search according to the member b. I am using multiset with that.

Problem is, that when another object comes, is checked according to sorting class, and fulfills to return true (to be put higher than the object that is compared to), it might also fulfill with another one being one step higher. How can I introduce here, that it should check with as many previous objects, and go as high as is only possible? (to obtain really well sorted multiset!) Below how I wrote the sorting class. Please provide me with some nice idea!

And I had problems to introduce indentations for this part of code, anyone can help me to edit please? it's unreadable.

std::multiset<classA , classA ::SortClass> mydata;

    class SortClass
        {
        public:
            bool operator() (const classA &pt1, const classA &pt2)
                {
                if(pt1.a < pt2.a)
                    {
                    if(abs(pt1.a-pt2.a) < 0.01)
                        {
                        if(pt1.b > pt2.b)
                            return true;
                        else 
                            return false;
                        }
                    else 
                        return true;
                    }
                else
                    return false;
                }
        };

EDIT:

Paste this and see what I want and cannot achieve. I want it sorted increasing with m_a and decreasing with m_b if m_a are the same (you can put there equality or inequality, does not work either)

#include <iostream>
#include <set>
using namespace std;


class classA
    {
    protected:
        double m_b;
        double m_a;

    public:
        double get_b() {return m_b;}
        double get_a() {return m_a;}
        void set_b(double b) {m_b = b;}
        void set_a(double a) {m_a = a;}    

        class SortClass
            {
            public:
                bool operator() (const classA &pt1, const classA &pt2)
                    {
                    if(pt1.m_a < pt2.m_a)
                        {
                        if(pt2.m_a - pt1.m_a == 0)
                            return (pt1.m_b > pt2.m_b);
                        else
                            return (pt1.m_a < pt2.m_a);
                        }
                    else
                        return false;
                    }
            };
    };
int main()
    {

    std::multiset<classA, classA::SortClass> mydata;
    classA objA;
    for(int i=0; i<100;i++)
        {
        objA.set_a(rand() %100);
        objA.set_b(rand() %10);
        mydata.insert(objA);
        }

    return 0;
    }
War es hilfreich?

Lösung

Let's stop using true, and false and return the bool expressions. It make your code so much easier to read.

    bool sortClassA (const classA &pt1, const classA &pt2)
    {
        if(pt2.m_a == pt1.m_a) {
            return pt1.m_b > pt2.m_b;
        }
        return pt1.m_a > pt2.m_a;
    };

Also, you created this issue you have with the threshold. Simple lower the threshold to something close to zero and the issue will go away, else you'll have to live with it. I don't see any other way.

Andere Tipps

Unfortunately, thresholds don't work well with STL ordered containers. A requisite for the ordering criterion is that it defines a strict weak ordering; this means, amongst other things, that if a == b and b == c, then a == b (== meaning here equivalent; this is, neither greater nor lesser than, not necessarily equal).

We might have pt1.a = 0.004, pt2.a = 0.01 and pt3.a = 0.016; according to your criterion, pt1.a == pt2.a and pt2.a == pt3.a, but pt1.a =\= pt3.a. Your ordering criterion does not meet the prerequisites for std::multiset and you cannot use it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top