473,387 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ordering container contents using more than one key

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<Rectanglemy_list;

The main problem is to construct an R+ Tree.

Aug 19 '07 #1
7 1244
naoki wrote:
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<Rectanglemy_list;
Sure. Just define operator< as you want.
Aug 19 '07 #2
On Aug 19, 8:33 pm, red floyd <no.s...@here.dudewrote:
naoki wrote:
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<Rectanglemy_list;

Sure. Just define operator< as you want.
operator< returns bool...

bool Rectangle::operator<( 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

Aug 19 '07 #3
naoki wrote:
On Aug 19, 8:33 pm, red floyd <no.s...@here.dudewrote:
>naoki wrote:
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<Rectanglemy_list;

Sure. Just define operator< as you want.

operator< returns bool...

bool Rectangle::operator<( 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::operator<( 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

Aug 20 '07 #4
On Aug 19, 9:10 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
naoki wrote:
On Aug 19, 8:33 pm, red floyd <no.s...@here.dudewrote:
naoki wrote:
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<Rectanglemy_list;
Sure. Just define operator< as you want.
operator< returns bool...
bool Rectangle::operator<( 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::operator<( 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

Aug 20 '07 #5
Kai-Uwe Bux wrote:
bool Rectangle::operator<( 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::operator<(const Rectangle& r) const
{
if(x == r.x) return y < r.y;
return x < r.x;
}
Aug 20 '07 #6
Kai-Uwe Bux wrote:
Juha Nieminen wrote:
>Kai-Uwe Bux wrote:
>>bool Rectangle::operator<( 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.
<nitI hope that you don't have a habit of using the "dot" operator
with 'this', but the "arrow", as in 'this->x' ... </nit>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 20 '07 #7
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
>Juha Nieminen wrote:
>>Kai-Uwe Bux wrote:
bool Rectangle::operator<( 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.

<nitI 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
Aug 20 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Xamalek | last post by:
Greetings, I have a question. When using an STL container, does deleting a container of pointers also call delete on the (contained) pointers? For example, if I have (ignore the fluff, it is...
2
by: Ney André de Mello Zunino | last post by:
Hello. The issue is quite known: you have a block-level container holding a set of floated elements and you need that the container's content height take the floated elements' dimensions into...
1
by: Dhawal Patel via .NET 247 | last post by:
Hi, I'm looking for .Net equivalent of Frame Control. ThoughGroupBox and Panel controls are available in .Net, there is aslight change in behaviour. Both GroupBox & Panel have relativetab orders. But...
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
21
by: John Salerno | last post by:
If I want to make a list of four items, e.g. L = , and then figure out if a certain element precedes another element, what would be the best way to do that? Looking at the built-in list...
0
by: Dervish | last post by:
Here is pseudocode: typedef int CarID; typedef RWTPtrHashSet<CarID,HashAdaptorCarID, CarIDEqualTo> container_type; typedef RWTPtrHashSetIterator<CarID,HashAdaptorCarID, CarIDEqualTo>...
5
by: =?Utf-8?B?VG9t?= | last post by:
Cannot not seem to make any sense of the order that my key/values end up in when added to the Hashtable...ideally, I would like to be able to sort the keys/values...but not thinking it is possible....
2
by: ge5talt | last post by:
Hi all, I am looking for a switch that will style the height of a parent element to a specific % if the height of the contents is less than the height of the container (it doesnt spill out), or...
13
by: LC's No-Spam Newsreading account | last post by:
I have happily lived for 15 years (our first web server started in 1993) without CSS, but I now decided to learn and experiment with it. My personal preference has always been that the width of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.