473,659 Members | 3,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing duplicates from container,

I have a container of pointers. It is possible for two pointers to point to
the same element. I want to remove duplicates.

I am open to which container is best for this.

I thought of using std::set, but my elements do not have a '<' operator, as
it does not make sense. I could add an integer index to the data structure
so that each element has a unique index associated with it and then override
'<' to sort by index, but I am not sure if this is the best way.

Any advice appreciated.
Jul 5 '06 #1
7 3361
vsgdp wrote:
I have a container of pointers. It is possible for two pointers to point to
the same element. I want to remove duplicates.

I am open to which container is best for this.

I thought of using std::set, but my elements do not have a '<' operator, as
it does not make sense. I could add an integer index to the data structure
so that each element has a unique index associated with it and then override
'<' to sort by index, but I am not sure if this is the best way.

Any advice appreciated.
Why can't you use std::set again? You could simply use addresses (i.e.
the value held by the pointer) as your sorting criterion. No duplicates
would be allowed (or did you mean the pointers could be different but
the pointees the same?). E.g.,

struct S { /*...*/ };
std::set<S*ss; // Ok

Cheers! --M

Jul 5 '06 #2
mlimber wrote:
vsgdp wrote:
>I have a container of pointers. It is possible for two pointers to point to
the same element. I want to remove duplicates.

I am open to which container is best for this.

I thought of using std::set, but my elements do not have a '<' operator, as
it does not make sense. I could add an integer index to the data structure
so that each element has a unique index associated with it and then override
'<' to sort by index, but I am not sure if this is the best way.

Any advice appreciated.

Why can't you use std::set again? You could simply use addresses (i.e.
the value held by the pointer) as your sorting criterion. No duplicates
would be allowed (or did you mean the pointers could be different but
the pointees the same?). E.g.,

struct S { /*...*/ };
std::set<S*ss; // Ok

Cheers! --M
A set would work fine, especially if you need to maintain at all times
the no-duplicates condition. If you only need to process the data once
or occasionally to remove all duplicates, you can use a vector, sort it,
and then remove duplicates. Look at std::sort and std::unique or
std::unique_cop y.

Mark
Jul 5 '06 #3
Why can't you use std::set again? You could simply use addresses (i.e.
the value held by the pointer) as your sorting criterion.
struct S { /*...*/ };
std::set<S*ss; // Ok
Thanks, for some reason I was thinking it wouldn't make sense to use '<' on
pointers, since what does it mean for a pointer to be less than another
pointer. But then I remembered pointers are basically address numbers, and
so this would be defined.
Jul 5 '06 #4
mlimber <ml*****@gmail. comwrote:
struct S { /*...*/ };
std::set<S*ss; // Ok
Is it really ok? I vaguely recall something about not being allowed
to compare pointers (except for (in)equality), unless they belong to the
same array .. ?

regards
--
jb

(reply address in rot13, unscramble first)
Jul 5 '06 #5
>struct S { /*...*/ };
std::set<S*s s; // Ok

Is it really ok? I vaguely recall something about not being allowed to
compare pointers (except for (in)equality), unless they belong to the same
array .. ?
I found something in an old post:

" Also note that others
have pointed out that comparing addresses of objects
that are not part of the same aggregrate (e.g. 'struct'
or array) with comparison operator othere than '=='
is undefined. "

http://groups.google.com/group/comp....36952bfb23022d

My "set" will actually store pointers to elements that are in one big array,
so I guess it is okay for me to use set since I'll be comparing addresses of
elements that are part of the same array.

Jul 5 '06 #6
vsgdp <he***@null.com wrote:
>>struct S { /*...*/ };
std::set<S*ss ; // Ok
> Is it really ok? I vaguely recall something about not being
allowed to compare pointers (except for (in)equality), unless they
belong to the same array .. ?
I found something in an old post:

" Also note that others
have pointed out that comparing addresses of objects
that are not part of the same aggregrate (e.g. 'struct'
or array) with comparison operator othere than '=='
is undefined. "

http://groups.google.com/group/comp....36952bfb23022d

My "set" will actually store pointers to elements that are in one big
array, so I guess it is okay for me to use set since I'll be
comparing addresses of elements that are part of the same array.
Yip, agreed.
--
jb

(reply address in rot13, unscramble first)
Jul 6 '06 #7
In message <dZWqg.3494$5K2 .798@fed1read03 >, vsgdp <he***@null.com >
writes
>
>>struct S { /*...*/ };
std::set<S*ss ; // Ok

Is it really ok? I vaguely recall something about not being allowed to
compare pointers (except for (in)equality), unless they belong to the same
array .. ?

I found something in an old post:

" Also note that others
have pointed out that comparing addresses of objects
that are not part of the same aggregrate (e.g. 'struct'
or array) with comparison operator othere than '=='
is undefined. "

http://groups.google.com/group/comp..../thread/10e543
c4d33dd566/e136952bfb23022 d?lnk=st&q=comp aring+pointers+ in+c%2B%2B&rnum =
5&hl=en#e13695 2bfb23022d

My "set" will actually store pointers to elements that are in one big array,
so I guess it is okay for me to use set since I'll be comparing addresses of
elements that are part of the same array.
That's not a problem anyway, because std::set uses std::less as its
default comparison functor, and the standard (20.3.3/8) guarantees that
std::less defines a total order on pointer types even when operator <
doesn't.
--
Richard Herring
Jul 10 '06 #8

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

Similar topics

22
35470
by: christof hoeke | last post by:
hello, this must have come up before, so i am already sorry for asking but a quick googling did not give me any answer. i have a list from which i want a simpler list without the duplicates an easy but somehow contrived solution would be >>> a = >>> d = {}.fromkeys(a) >>> b = d.keys()
2
3668
by: Iain | last post by:
Hi I have inherited a web app with the following table structure, and need to produce a table without any duplicates. Email seems like the best unique identifier - so only one of each e-mail address should be in the table. Following http://www.sqlteam.com/item.asp?ItemID=3331 I have been able to get a duplicate count working: select Email, count(*) as UserCount
20
3330
by: Rubinho | last post by:
I've a list with duplicate members and I need to make each entry unique. I've come up with two ways of doing it and I'd like some input on what would be considered more pythonic (or at least best practice). Method 1 (the traditional approach) for x in mylist: if mylist.count(x) > 1:
6
3467
by: M B HONG 20 | last post by:
Hi all - I was wondering if Javascript has a way to easily remove duplicates from a string. For example, if I had a string: "car truck car truck truck tree post post tree" it should turn into: "car truck tree post"
4
3618
by: Drew | last post by:
I have a permission tracking app that I am working on, and I have made the insert page for it. I am having issues on how to prevent duplicates from getting entered. Currently the interface for the app has a mixture of select boxes, list boxes and checkboxes. The form submits the page to processAIMR.asp and then does the inserting. I am using a loop to insert a new record for each checkbox checked or listbox entry selected. My...
0
2578
by: makthar | last post by:
In your query use DISTINCT SELECT DISTINCT CITY FROM <tablename> WHERE STATE='<state name>'. This will bring only one of each city from the table. >-----Original Message----- >I'm getting a list of addresses from a database and binding them to a >datagrid. I have a dropdown list that allows the user
16
4168
by: tyrfboard | last post by:
I've been searching for awhile now on how to remove duplicates from a table within an Access db and have found plenty of articles on finding or deleting duplicates. All I want to do is remove them from within an SQL query - leaving one of the records behind of course. I have a mailing list comprised of a union query that gets records from two separate tables. I want to be able to run a query that removes one (or more) of the duplicated...
5
11262
by: maphew | last post by:
Hello, I have some lists for which I need to remove duplicates. I found the sets.Sets() module which does exactly this, but how do I get the set back out again? # existing input: A,B,B,C,D # desired result: A,B,C,D import sets
4
13958
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script, where I want to eliminate the duplicates in an array (String). for (int k=0; k<myLength; k++){ boolean isthere=false; for (int l=0; l<sizeres; l++){ if (res.equals(my)){
0
8337
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
8851
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
8748
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
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
4175
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...
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.