By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

removing duplicates from container,

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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_copy.

Mark
Jul 5 '06 #3

P: n/a
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

P: n/a
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

P: n/a
>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.

Jul 5 '06 #6

P: n/a
vsgdp <he***@null.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 .. ?
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

P: n/a
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/e136952bfb23022d?lnk=st&q=comparing+pointers+in+c% 2B%2B&rnum=
5&hl=en#e136952bfb23022d

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 discussion thread is closed

Replies have been disabled for this discussion.