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

problems with set::insert

P: n/a
Rob
I'm trying to use the set<my_class>::insert( my_class const & )
method, but have run into some problems. It seems like I'm seeing it
insert an object when a similar object is already in the set.

After calling insert(), I see the .second of the return value set to
"true", meaning it was inserted. The size() of the set increases by
1, but the count() of the object I insert doesn't change (it was 1, it
is still 1). I do a quick check and see that my new object is not
less than the one already in the set, and the one already in the set
is not less than the new object.

Does that make any sense? I can't really post all the code, but I
could post some pseudocode if that helps...

I tried reproducing outside the program but didn't have much luck.

Does anybody have a suggestion for something in general I might be
doing wrong? My operator< is defined and looks fine to me. I'm using
GCC 3.4.6.

Thanks!

Rob

Mar 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Rob wrote:
I'm trying to use the set<my_class>::insert( my_class const & )
method, but have run into some problems. It seems like I'm seeing it
insert an object when a similar object is already in the set.

After calling insert(), I see the .second of the return value set to
"true", meaning it was inserted. The size() of the set increases by
1, but the count() of the object I insert doesn't change (it was 1, it
is still 1). I do a quick check and see that my new object is not
less than the one already in the set, and the one already in the set
is not less than the new object.

Does that make any sense? I can't really post all the code, but I
could post some pseudocode if that helps...

I tried reproducing outside the program but didn't have much luck.

Does anybody have a suggestion for something in general I might be
doing wrong? My operator< is defined and looks fine to me. I'm using
GCC 3.4.6.
Let's see some code, especially the definition of 'my_class' and its
operator<. While we're lookint at it, read up on "strict weak ordering".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 7 '07 #2

P: n/a
Rob
On Mar 7, 9:15 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rob wrote:
I'm trying to use the set<my_class>::insert( my_class const & )
method, but have run into some problems. It seems like I'm seeing it
insert an object when a similar object is already in the set.
After calling insert(), I see the .second of the return value set to
"true", meaning it was inserted. The size() of the set increases by
1, but the count() of the object I insert doesn't change (it was 1, it
is still 1). I do a quick check and see that my new object is not
less than the one already in the set, and the one already in the set
is not less than the new object.
Does that make any sense? I can't really post all the code, but I
could post some pseudocode if that helps...
I tried reproducing outside the program but didn't have much luck.
Does anybody have a suggestion for something in general I might be
doing wrong? My operator< is defined and looks fine to me. I'm using
GCC 3.4.6.

Let's see some code, especially the definition of 'my_class' and its
operator<. While we're lookint at it, read up on "strict weak ordering".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I'm very sorry for not being able to post the actual code - it's not
public and all that...

struct my_class {
std::string mString1;
std::string mString2;
double mDouble;
std::vector<unsigned charmVector1;
std::vector<unsigned charmVector2;

my_class();
my_class( string,string,double );
my_class( string, string, time, std::vector<unsigned char);
my_class( string, std::vector<unsigned char);
my_class( my_class const & other );

bool operator< ( my_class const & other ) const;
bool operator== ( my_class const & other ) const;
};

bool my_class::operator < ( my_class const & other ) const
{
return ( mString1 < other.mString1
|| ( !(other.mString1 < mString1)
&& mString2 < other.mString2 )
|| ( !(other.mString2 < mString2)
&& mDouble < other.mDouble )
|| ( !(other.mDouble < mDouble)
&& mVector1 < other.mVector1 )
|| ( !(other.mVector1 < mVector1)
&& mVector2 < other.mVector2 ) );
}

Some notes/thoughts:
- Sorry for formatting; I'm using google groups which doesn't have
fixed-width fonts
- I removed most "const &" from the constructor declarations
- The two byte vectors are empty for the situation I'm describing
- The two strings are equal as well
- The two doubles appear to be equal (with std::fixed), and I believe
they are truly equal
- The operator< returns false either way - I take it to mean the
objects are equivalent
- The fact that count(obj) returns 1 before the insert makes me very
surprised that the
object is inserted!

Thanks for your help!

Rob

Mar 7 '07 #3

P: n/a
Rob wrote:
On Mar 7, 9:15 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Rob wrote:
>>I'm trying to use the set<my_class>::insert( my_class const & )
method, but have run into some problems. It seems like I'm seeing
it insert an object when a similar object is already in the set.
>>After calling insert(), I see the .second of the return value set to
"true", meaning it was inserted. The size() of the set increases by
1, but the count() of the object I insert doesn't change (it was 1,
it is still 1). I do a quick check and see that my new object is
not less than the one already in the set, and the one already in
the set is not less than the new object.
>>Does that make any sense? I can't really post all the code, but I
could post some pseudocode if that helps...
>>I tried reproducing outside the program but didn't have much luck.
>>Does anybody have a suggestion for something in general I might be
doing wrong? My operator< is defined and looks fine to me. I'm
using GCC 3.4.6.

Let's see some code, especially the definition of 'my_class' and its
operator<. While we're lookint at it, read up on "strict weak
ordering".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

I'm very sorry for not being able to post the actual code - it's not
public and all that...
[..code..]

Some notes/thoughts:
- Sorry for formatting; I'm using google groups which doesn't have
fixed-width fonts
- I removed most "const &" from the constructor declarations
- The two byte vectors are empty for the situation I'm describing
- The two strings are equal as well
- The two doubles appear to be equal (with std::fixed), and I believe
they are truly equal
- The operator< returns false either way - I take it to mean the
objects are equivalent
- The fact that count(obj) returns 1 before the insert makes me very
surprised that the
object is inserted!
OK, you're right about equivalency requirement. If comp(a,b) == false
and comp(b,a) == false, they are equivalent. That leaves only the
procedure that takes place upon insertion. It should only insert if
there is no equivalent element in the set. Unless you can come up with
the code we can copy-paste-compile-run, the only speculation remains
here is that either your library is at fault (insertion does not check
equivalency [correctly]), or there is another error elsewhere in your
program.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 7 '07 #4

P: n/a
Rob
On Mar 7, 10:45 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rob wrote:
On Mar 7, 9:15 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Rob wrote:
I'm trying to use the set<my_class>::insert( my_class const & )
method, but have run into some problems. It seems like I'm seeing
it insert an object when a similar object is already in the set.
>After calling insert(), I see the .second of the return value set to
"true", meaning it was inserted. The size() of the set increases by
1, but the count() of the object I insert doesn't change (it was 1,
it is still 1). I do a quick check and see that my new object is
not less than the one already in the set, and the one already in
the set is not less than the new object.
>Does that make any sense? I can't really post all the code, but I
could post some pseudocode if that helps...
>I tried reproducing outside the program but didn't have much luck.
>Does anybody have a suggestion for something in general I might be
doing wrong? My operator< is defined and looks fine to me. I'm
using GCC 3.4.6.
Let's see some code, especially the definition of 'my_class' and its
operator<. While we're lookint at it, read up on "strict weak
ordering".
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I'm very sorry for not being able to post the actual code - it's not
public and all that...
[..code..]
Some notes/thoughts:
- Sorry for formatting; I'm using google groups which doesn't have
fixed-width fonts
- I removed most "const &" from the constructor declarations
- The two byte vectors are empty for the situation I'm describing
- The two strings are equal as well
- The two doubles appear to be equal (with std::fixed), and I believe
they are truly equal
- The operator< returns false either way - I take it to mean the
objects are equivalent
- The fact that count(obj) returns 1 before the insert makes me very
surprised that the
object is inserted!

OK, you're right about equivalency requirement. If comp(a,b) == false
and comp(b,a) == false, they are equivalent. That leaves only the
procedure that takes place upon insertion. It should only insert if
there is no equivalent element in the set. Unless you can come up with
the code we can copy-paste-compile-run, the only speculation remains
here is that either your library is at fault (insertion does not check
equivalency [correctly]), or there is another error elsewhere in your
program.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Well, I tried to extract just the classes involved, and went to some
effort to construct my objects the same way, but I just couldn't
reproduce it outside the original code. So I set up the workaround to
check count(obj) before calling insert(), which seems to work.

Thanks for your help!

Rob

Mar 8 '07 #5

P: n/a
>
Well, I tried to extract just the classes involved, and went to some
effort to construct my objects the same way, but I just couldn't
reproduce it outside the original code. So I set up the workaround to
check count(obj) before calling insert(), which seems to work.
It's likely that your original code has some subtle bug, but if it
'seems to work' who am I to quibble.

Usually in trying to get a handle on this kind of issue, it's better to
start removing code from the bugged program, instead of trying to build
upto the problem code from scratch.
Thanks for your help!

Rob
Mar 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.