473,498 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with set::insert

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
5 2720
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
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
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
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9476
by: Xenos | last post by:
I have elements of vector I need to add to a set. Can I just do (ignoring namespace): copy(v.begin(), v.end(), inserter(s, s.end())); Where, v is a vector and s is a set, and both have the...
6
6966
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: ...
1
2486
by: jimmy | last post by:
Hi, I am getting a Fatal signal with std::set::insert(). I'm really not sure what to try next: Here is my code: std::cout << "max size: " << _indices.max_size() << std::endl; std::cout <<...
8
6985
by: Johannes A. Brunner | last post by:
Got a simple problem. I code some site and because Im a freak I made my own session-handling. When a user open up my site it will check if there is a ssid in the url if not generate one. this will...
12
3492
by: Marcus Kwok | last post by:
I am not sure if this is something that is covered by the Standard, or if it's an implementation detail of my Standard Library. I am reading in a large amount of data into a std::set. There is...
4
14994
by: Mark Olbert | last post by:
I am struggling with trying to retrieve the value of an autoincrement identity field after a DetailsView Insert operation. The DetailsView is bound to an SqlDataSource control. So far as I can...
5
5078
by: zensunni | last post by:
I like to use this method when inserting into a database: Set Insert = Server.CreateObject("ADODB.Recordset") Insert.Open "test", objConn, 1, 3, 2 Insert.AddNew Insert("FIRSTNAME") = "Joe"...
2
1700
by: puzzlecracker | last post by:
I meant something else entirely. I was refering to algorithms, such as find_if where passed predicate, which implemented operator () for comparison. I assume set uses operator == to locate elements...
2
3863
by: Olumide | last post by:
Hello, I've got this nice inner class that I'm holds a set of "FrontPoint" objects as shown below. Unfortunately, the find and insert methods trigger massive C2784 errors. Would someone please...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6890
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...
0
7379
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...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1423
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 ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
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...

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.