problems with set::insert

R

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
 
V

Victor Bazarov

Rob said:
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
 
R

Rob

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

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 char> mVector1;
std::vector<unsigned char> mVector2;

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::eek:perator < ( 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
 
V

Victor Bazarov

Rob said:
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

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
 
R

Rob

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

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
 
J

John Harrison

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.
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top