problems with set::insert

Discussion in 'C++' started by Rob, Mar 7, 2007.

  1. Rob

    Rob Guest

    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
     
    Rob, Mar 7, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Mar 7, 2007
    #2
    1. Advertising

  3. Rob

    Rob Guest

    On Mar 7, 9:15 am, "Victor Bazarov" <> wrote:
    > 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 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
     
    Rob, Mar 7, 2007
    #3
  4. Rob wrote:
    > On Mar 7, 9:15 am, "Victor Bazarov" <> wrote:
    >> 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
     
    Victor Bazarov, Mar 7, 2007
    #4
  5. Rob

    Rob Guest

    On Mar 7, 10:45 am, "Victor Bazarov" <> wrote:
    > Rob wrote:
    > > On Mar 7, 9:15 am, "Victor Bazarov" <> wrote:
    > >> 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
     
    Rob, Mar 8, 2007
    #5
  6. >
    > 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
    >
     
    John Harrison, Mar 8, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cannontrodder
    Replies:
    1
    Views:
    764
    cannontrodder
    Jul 25, 2006
  2. Replies:
    1
    Views:
    480
  3. Replies:
    1
    Views:
    1,873
    Albert Hopkins
    Dec 6, 2008
  4. eagle

    Insert & Insert/Repeat buttons in a detailsview

    eagle, Jul 12, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    1,087
    eagle
    Jul 12, 2007
  5. Feege
    Replies:
    0
    Views:
    502
    Feege
    Dec 20, 2005
Loading...

Share This Page