473,791 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

set of unordered objects

How can I build a set of unordered objects lets say POINT_2D objects?
The POINT_2D class does not have a consistent operator<.

Have a nice day,
Mihai.

Jul 23 '05 #1
9 1679
mihai wrote:
How can I build a set of unordered objects lets say POINT_2D objects?
The POINT_2D class does not have a consistent operator<.

Have a nice day,
Mihai.

Either create an 'operator<' for POINT_2D (whatever that is),
or use 'vector' or 'list' instead of 'set'.

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #2
mihai wrote:
How can I build a set of unordered objects lets say POINT_2D objects?
The POINT_2D class does not have a consistent operator<.


I'm not sure what you mean. In a set (if you mean std::set, which I assume
since you mention operator<), the elements are always ordered. Are you sure
you really want a set?

Jul 23 '05 #3

Rolf Magnus wrote:
mihai wrote:
How can I build a set of unordered objects lets say POINT_2D objects? The POINT_2D class does not have a consistent operator<.
I'm not sure what you mean. In a set (if you mean std::set, which I

assume since you mention operator<), the elements are always ordered. Are you sure you really want a set?


I'm assuming he wants a set to be guaranteed of there not being any
duplicates. In order to do so, the std::Set stores obects in order, so
it can quickly find out if the element is already in the set. So, if
you really want the property of no duplicate items, define a consistent
operator<, for instsance, pointA.length() <pointB.lenght( ), where
length() would return sqrt(x*x + y*y).

Jul 23 '05 #4
How can I build a set of unordered objects lets say POINT_2D objects?


See if your compiler vendor provides unordered_set (hash_set) or equivalent
defined by an == operator. I know it is non-standard (and eventually will be
standard) but this container matches your requirements.

Stephen Howe
Jul 23 '05 #5
In message <11************ **********@f14g 2000cwb.googleg roups.com>, Mark
Stijnman <m.**********@t nw.utwente.nl> writes

Rolf Magnus wrote:
mihai wrote:
> How can I build a set of unordered objects lets say POINT_2Dobjects? > The POINT_2D class does not have a consistent operator<.


I'm not sure what you mean. In a set (if you mean std::set, which I

assume
since you mention operator<), the elements are always ordered. Are

you sure
you really want a set?


I'm assuming he wants a set to be guaranteed of there not being any
duplicates. In order to do so, the std::Set stores obects in order, so
it can quickly find out if the element is already in the set. So, if
you really want the property of no duplicate items, define a consistent
operator<, for instsance, pointA.length() <pointB.lenght( ), where
length() would return sqrt(x*x + y*y).


That won't work for std::set, which needs a strict weak ordering.

With your definition, you can have objects a, b for which none of a<b,
b<a, a==b is true, which will sooner or later lead to great confusion.

Better to define it something like std::pair does:
a.x < b.x || (!(b.x < a.x) && a.y < b.y)

--
Richard Herring
Jul 23 '05 #6
Richard Herring wrote:
I'm assuming he wants a set to be guaranteed of there not being any
duplicates. In order to do so, the std::Set stores obects in order, so
it can quickly find out if the element is already in the set. So, if
you really want the property of no duplicate items, define a consistent
operator<, for instsance, pointA.length() <pointB.lenght( ), where
length() would return sqrt(x*x + y*y).
That won't work for std::set, which needs a strict weak ordering.

With your definition, you can have objects a, b for which none of a<b,
b<a, a==b is true, which will sooner or later lead to great confusion.


Do you have an example of a combination of two vectors of which neither is
shorter than the other and still their length is not equal?
Better to define it something like std::pair does:
a.x < b.x || (!(b.x < a.x) && a.y < b.y)


Kind of lexical order.

Jul 23 '05 #7
In message <d4************ *@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Richard Herring wrote:
I'm assuming he wants a set to be guaranteed of there not being any
duplicates . In order to do so, the std::Set stores obects in order, so
it can quickly find out if the element is already in the set. So, if
you really want the property of no duplicate items, define a consistent
operator<, for instsance, pointA.length() <pointB.lenght( ), where
length() would return sqrt(x*x + y*y).


That won't work for std::set, which needs a strict weak ordering.

With your definition, you can have objects a, b for which none of a<b,
b<a, a==b is true, which will sooner or later lead to great confusion.


Do you have an example of a combination of two vectors of which neither is
shorter than the other and still their length is not equal?


No, why would I?

If a = (0,1) and b = (1,0), then for most useful definitions of points
in 2D space they are not equal. Nevertheless if operator< is defined in
terms of length() neither of the assertions a<b, b<a is true, so they
are in the same equivalence class. Consequently:

#include <cmath>
#include <iostream>
#include <ostream>
#include <set>

struct Point {
Point(int xx, int yy) : x(xx), y(yy) {}
double length() const { return std::sqrt(x*x + y*y); }
int x, y;
};

bool operator<(Point const & a, Point const & b)
{ return a.length() < b.length(); }

int main()
{
Point a(0,1);
Point b(1,0);
std::set<Point> s;
s.insert(a);
std::cout << s.count(b) << '\n';
}
Better to define it something like std::pair does:
a.x < b.x || (!(b.x < a.x) && a.y < b.y)


Kind of lexical order.


--
Richard Herring
Jul 23 '05 #8
Richard Herring wrote:
In message <d4************ *@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Richard Herring wrote:
I'm assuming he wants a set to be guaranteed of there not being any
duplicate s. In order to do so, the std::Set stores obects in order, so
it can quickly find out if the element is already in the set. So, if
you really want the property of no duplicate items, define a consistent
operator< , for instsance, pointA.length() <pointB.lenght( ), where
length() would return sqrt(x*x + y*y).

That won't work for std::set, which needs a strict weak ordering.

With your definition, you can have objects a, b for which none of a<b,
b<a, a==b is true, which will sooner or later lead to great confusion.
Do you have an example of a combination of two vectors of which neither is
shorter than the other and still their length is not equal?


No, why would I?


Because you claim that this is possible.
If a = (0,1) and b = (1,0), then for most useful definitions of points
in 2D space they are not equal.
Above, you wrote: "That won't work for std::set" and "With your definition,
you can have objects a, b for which none of a<b, b<a, a==b is true", and
that's not right. We can argue about the usefulness of that operator<, but
that's another story.
Nevertheless if operator< is defined in
terms of length() neither of the assertions a<b, b<a is true,
Yes, that's because they are considered equal in that case.
so they are in the same equivalence class. Consequently:

#include <cmath>
#include <iostream>
#include <ostream>
#include <set>

struct Point {
Point(int xx, int yy) : x(xx), y(yy) {}
double length() const { return std::sqrt(x*x + y*y); }
int x, y;
};

bool operator<(Point const & a, Point const & b)
{ return a.length() < b.length(); }

int main()
{
Point a(0,1);
Point b(1,0);
std::set<Point> s;
s.insert(a);
std::cout << s.count(b) << '\n';
}


If you have std::set<std::s tring> and use a case-insensitive comparison, you
can also try to put two different strings in a set and only one will be
stored.

Jul 23 '05 #9
In message <d4************ *@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Richard Herring wrote:
In message <d4************ *@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Richard Herring wrote:

>I'm assuming he wants a set to be guaranteed of there not being any
>duplicates . In order to do so, the std::Set stores obects in order, so
>it can quickly find out if the element is already in the set. So, if
>you really want the property of no duplicate items, define a consistent
>operator <, for instsance, pointA.length() <pointB.lenght( ), where
>length() would return sqrt(x*x + y*y).

That won't work for std::set, which needs a strict weak ordering.

With your definition, you can have objects a, b for which none of a<b,
b<a, a==b is true, which will sooner or later lead to great confusion.

Do you have an example of a combination of two vectors of which neither is
shorter than the other and still their length is not equal?
No, why would I?


Because you claim that this is possible.


No, I do not. I claim that you can have two vectors of which neither is
shorter than the other yet their *directions* are not equal.
If a = (0,1) and b = (1,0), then for most useful definitions of points
in 2D space they are not equal.


Above, you wrote: "That won't work for std::set" and "With your definition,
you can have objects a, b for which none of a<b, b<a, a==b is true", and
that's not right.


But it is. That operator < defines equivalence classes which don't
correspond to vector equality. Therefore (the conventional meaning of)
== is inconsistent with <.
We can argue about the usefulness of that operator<, but
that's another story.
It's fine (ie it's a strict weak ordering) so far as the inner workings
of std::set go, but it defines an equivalence which doesn't match the
usual interpretation of vector equality. When looking things up in sets
or maps, it's the equivalence, not the ordering, which is important.
Nevertheless if operator< is defined in
terms of length() neither of the assertions a<b, b<a is true,
Yes, that's because they are considered equal in that case.


Not equal, equivalent. My point is that this equivalence is not equality
because information is discarded.
so they are in the same equivalence class. Consequently:

#include <cmath>
#include <iostream>
#include <ostream>
#include <set>

struct Point {
Point(int xx, int yy) : x(xx), y(yy) {}
double length() const { return std::sqrt(x*x + y*y); }
int x, y;
};

bool operator<(Point const & a, Point const & b)
{ return a.length() < b.length(); }

int main()
{
Point a(0,1);
Point b(1,0);
std::set<Point> s;
s.insert(a);
std::cout << s.count(b) << '\n';
}


If you have std::set<std::s tring> and use a case-insensitive comparison, you
can also try to put two different strings in a set and only one will be
stored.

Of course. The difference is that case-insensitive "equality" is
sometimes a useful way of defining equivalence classes for strings.
Direction-insensitive "equality" is not a correspondingly useful concept
for vectors.

--
Richard Herring
Jul 23 '05 #10

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

Similar topics

1
5384
by: Michael Champagne | last post by:
Does anyone have any examples or links to examples of generating a simple unordered list menu with multiple levels from an XML file? We are redesigning the navigation on our website and I was hoping to make this as flexible as possible. Thanks, Mike
66
5038
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
3
3834
by: Kevin Campbell | last post by:
Say I have the following XML spec: <Book> <Title /> <Author /> <ISBN /> <Description /> </Book> Title, Author, ISBN and Description may appear in any order. Title,
4
2271
by: Justin Archie | last post by:
The subject says it all my friends. I have searched google to death about centering problems and for some reason no one seems to have the error I have. For the past few days I have been working on a website for a non-profit club and I and they were happy with the design and layout. I did not discover a problem with what I had until I was testing all browsers for compatibility. The following page...
1
30984
by: Nathan Sokalski | last post by:
I want to add a bulleted list to my document in ASP.NET. I know the HTML code, and I have used the HtmlGenericControl, but is there an ASP.NET object that can generate UL and LI tags from a set of ListItem objects, or maybe some other collection? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
3
3499
by: User | last post by:
Hi, Is it possible to transform Ordered/Unordered list into navigation dropdown menus? Is this effect achieved by CSS? or via Javascript? PLease advise Thanks.
2
2177
by: torweb | last post by:
I'm using an image for an unordered list, which works fine. The problem is, the image is also appearing in my numbered "ordered list." Here is my code for the unordered list:...and thanks in advance: ul { font-family: Arial; font-size: 12px; color: #424E51; font-style: normal line-height: 17px; font-weight: normal; font-variant: normal;
2
2224
by: asc4john | last post by:
When including an unordered list in an unordered list: Should it be included as a "list" item as in: <ul><liitem</ li><ul.... </ul<liitem</li</ul> or included in the LI element as in: <ul><liitem</li<li<ul... </ul></li></ul>? or does it matter?
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.