removing duplicates from container,

V

vsgdp

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.
 
M

mlimber

vsgdp said:
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
 
M

Mark P

mlimber said:
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
 
V

vsgdp

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.
 
J

Jakob Bieling

mlimber said:
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
 
V

vsgdp

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...pointers+in+c++&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.
 
J

Jakob Bieling

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...pointers+in+c++&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.

Yip, agreed.
 
R

Richard Herring

[QUOTE="vsgdp said:
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.lang.c++/browse_thread/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.
[/QUOTE]
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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top