ordering container contents using more than one key

N

naoki

Is it possible to sort a given container using two properties of the
content?

example:
class Rectangle {
int x, y;
int width, height;
};

std::list<Rectangle> my_list;

The main problem is to construct an R+ Tree.
 
R

red floyd

naoki said:
Is it possible to sort a given container using two properties of the
content?

example:
class Rectangle {
int x, y;
int width, height;
};

std::list<Rectangle> my_list;

Sure. Just define operator< as you want.
 
N

naoki

Sure. Just define operator< as you want.

operator< returns bool...

bool Rectangle::eek:perator<( const Rectangle& r ) {
return this.x < r.x && this.y < r.y;
}

would work?

sample output data R(y, x) I need:
R(0, 7)
R(0, 8)
R(0, 9)
R(1, 4)
R(1, 5)
R(1, 6)
R(2, 1)
R(2, 2)
R(2, 3)

TIA
 
K

Kai-Uwe Bux

naoki said:
operator< returns bool...

bool Rectangle::eek:perator<( const Rectangle& r ) {
return this.x < r.x && this.y < r.y;
}

would work?

Well, did you try?

sample output data R(y, x) I need:
R(0, 7)
R(0, 8)
R(0, 9)
R(1, 4)
R(1, 5)
R(1, 6)
R(2, 1)
R(2, 2)
R(2, 3)

Decide by primary value first; and only if those values tie, decide by
secondary value. E.g., with x as the primary value and y as the secondary
value, you could use something like:

bool Rectangle::eek:perator<( const Rectangle& r ) const {
if ( this.x < r.x ) {
return ( true );
}
if ( r.x < this.x ) {
return ( false );
}
// at this point, this.x == r.x
return ( this.y < r.y );
}


Best

Kai-Uwe Bux
 
N

naoki

Well, did you try?


Decide by primary value first; and only if those values tie, decide by
secondary value. E.g., with x as the primary value and y as the secondary
value, you could use something like:

bool Rectangle::eek:perator<( const Rectangle& r ) const {
if ( this.x < r.x ) {
return ( true );
}
if ( r.x < this.x ) {
return ( false );
}
// at this point, this.x == r.x
return ( this.y < r.y );

}

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -

Excuse me, I tried after post.
Thanks for the answer
 
J

Juha Nieminen

Kai-Uwe Bux said:
bool Rectangle::eek:perator<( const Rectangle& r ) const {
if ( this.x < r.x ) {
return ( true );
}
if ( r.x < this.x ) {
return ( false );
}
// at this point, this.x == r.x
return ( this.y < r.y );
}


bool Rectangle::eek:perator<(const Rectangle& r) const
{
if(x == r.x) return y < r.y;
return x < r.x;
}
 
K

Kai-Uwe Bux

Juha said:
bool Rectangle::eek:perator<(const Rectangle& r) const
{
if(x == r.x) return y < r.y;
return x < r.x;
}

That works, too. I guess it is a classic idiom.

I switched to the way I presented for the following two reasons:

a) I can see the logic more clearly since the control flow reflects the
ranking of attributes: the primary entries get compared first, if they do
not decide (return), the secondary entries decide. No need to come back to
the primary entries at a later point.

b) This approach does not use operator==. This can be important in generic
programming where you might only have a strict ordering represented by
operator<.

Now, I just made it a habit to code lexicographic comparison as above.


Best

Kai-Uwe Bux
 
V

Victor Bazarov

Kai-Uwe Bux said:
Juha said:
Kai-Uwe Bux said:
bool Rectangle::eek:perator<( const Rectangle& r ) const {
if ( this.x < r.x ) {
return ( true );
}
if ( r.x < this.x ) {
return ( false );
}
// at this point, this.x == r.x
return ( this.y < r.y );
}

[..]

Now, I just made it a habit to code lexicographic comparison as above.

<nit> I hope that you don't have a habit of using the "dot" operator
with 'this', but the "arrow", as in 'this->x' ... </nit>

V
 
K

Kai-Uwe Bux

Victor said:
Kai-Uwe Bux said:
Juha said:
Kai-Uwe Bux wrote:
bool Rectangle::eek:perator<( const Rectangle& r ) const {
if ( this.x < r.x ) {
return ( true );
}
if ( r.x < this.x ) {
return ( false );
}
// at this point, this.x == r.x
return ( this.y < r.y );
}

[..]

Now, I just made it a habit to code lexicographic comparison as above.

<nit> I hope that you don't have a habit of using the "dot" operator
with 'this', but the "arrow", as in 'this->x' ... </nit>

Ah, good catch. I should have run it by a compiler.


Thanks

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,051
Members
47,656
Latest member
rickwatson

Latest Threads

Top