Operator Overloading <

Hey guys, i'm doing a Bin Packing project and need some help with the Overloading of the operator < !
I have a list of objects pack, as a vector.
my packs objects are defined by height and width. For the method First Fit Decreasing Height i need a Decreasing order of my vector of objects pack.
I did like this and worked fine:
Class BinPacking i have this
1
2
sort(PackOBJ.begin(),PackOBJ.end());//order crescent 
reverse(PackOBJ.begin(),PackOBJ.end());//reverse to decreasing 

And in my Pack Class i have this
1
2
3
4
bool Pack::operator< (const Pack& other)
{
	return this->height < other.height;
}


And it's working, but i also wanted to order by width. In my case i have a pack that is 1x1 and 2x1 (width x height) and it is putting the 1x1 before the 2x1, i want use the bigger items first. I can't just get the total area, it have to be decreasing HEIGHT, but i want it also decreasing width. How can i do it?
i tried like this and did't work:
1
2
3
4
bool Pack::operator< (const Pack& other)
{
	return (this->height < other.height)&&(this->width < other.width);
}

and i fail...LOL
any help?
I'm stupid dumb!!!
i just figure it out!
1
2
3
4
5
6
7
bool Pack::operator< (const Pack& other)
{
	if (this->height == other.height)
		return (this->width < other.width);
	else
		return (this->height < other.height);
}


thanks anyway...
Your logic may not work if two pack's has both height and width equal.
Try this code, this may work for all the cases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

class Box
{
    int ht, wd;
    public:
        Box(int h, int w): ht(h), wd(w) {}
        char operator< (const Box &b)
        {
            if (this->ht == b.ht)
            {
                if (this->wd == b.wd)
                    return '=';
                else if(this->wd < b.wd)
                    return '<';
                else
                    return '>';
            }
            else if(this->ht < b.ht)
                return '<';
            else
                return '>';
        }
};

int main()
{
    Box b1(4, 1);
    Box b2(4, 0);

    switch (b1 < b2)
    {
        case '<':
            cout << "B1 < B2" << endl;
            break;
        case '>':
            cout << "B1 > B2" << endl;
            break;
        case '=':
            cout << "B1 == B2" << endl;
            break;
    }
    return 0;
}
Topic archived. No new replies allowed.