map & iterator

Discussion in 'C++' started by vertigo, Sep 22, 2004.

  1. vertigo

    vertigo Guest

    Hello
    I have strange problem, i have map<myclass1*,myclass2*,Compare>.
    Code:
    std::map<myclass1*,myclass2*,Compare> m=mymap;
    std::map<myclass1*,myclass2*,Compare>::const_iterator ci;

    printf("Elements: %d\n",m.size());
    for(ci=m.begin();ci!=m.end();ci++){
    printf("text\n");
    }

    The problem is that i receive:
    Elements: 5
    text
    text

    So i have added 5 elements to my map, but iterator iterates only thru
    two of them. Where could be the problem ?

    Thanx
    Michal
     
    vertigo, Sep 22, 2004
    #1
    1. Advertising

  2. vertigo wrote:
    > Hello
    > I have strange problem, i have map<myclass1*,myclass2*,Compare>.
    > Code:
    > std::map<myclass1*,myclass2*,Compare> m=mymap;
    > std::map<myclass1*,myclass2*,Compare>::const_iterator ci;
    >
    > printf("Elements: %d\n",m.size());
    > for(ci=m.begin();ci!=m.end();ci++){
    > printf("text\n");
    > }
    >
    > The problem is that i receive:
    > Elements: 5
    > text
    > text
    >
    > So i have added 5 elements to my map, but iterator iterates only thru
    > two of them. Where could be the problem ?


    Maybe your Compare is broken? What does it do?

    Yevgen
     
    Yevgen Muntyan, Sep 22, 2004
    #2
    1. Advertising

  3. "vertigo" <> wrote in message
    news:cirefa$9k5$...
    > Hello
    > I have strange problem, i have map<myclass1*,myclass2*,Compare>.
    > Code:
    > std::map<myclass1*,myclass2*,Compare> m=mymap;
    > std::map<myclass1*,myclass2*,Compare>::const_iterator ci;
    >
    > printf("Elements: %d\n",m.size());
    > for(ci=m.begin();ci!=m.end();ci++){
    > printf("text\n");
    > }
    >
    > The problem is that i receive:
    > Elements: 5
    > text
    > text
    >
    > So i have added 5 elements to my map, but iterator iterates only thru
    > two of them. Where could be the problem ?
    >
    > Thanx
    > Michal
    >


    That is a weird problem. The way to solve these problems is to post a
    complete program here. Then dozens of willing volunteers will compile and
    test your code and give you the answer pronto.

    John
     
    John Harrison, Sep 22, 2004
    #3
  4. vertigo

    vertigo Guest

    > Maybe your Compare is broken? What does it do?

    YES. When i declare/use map without Compare everything works fine.
    My compare:
    struct Compare{
    bool operator()(sha1 *sha1, sha1 *sha2) const{
    char *s1;
    char *s2;
    s1=sha1_sprintf_hex(sha1);
    s2=sha1_sprintf_hex(sha2);
    if (strncmp(s1,s2,40)==0){
    return false;
    }
    return true;
    }
    };

    Where sha1 object represents 160bit SHA1 value, sha1_sprintf_hex(sha)
    returns pointer to char table (nice formatted SHA1 string).

    What could be wrong with that ?

    Thanx
    Michal
     
    vertigo, Sep 22, 2004
    #4
  5. "vertigo" <> wrote in message
    news:cirj6o$2h2$...
    > > Maybe your Compare is broken? What does it do?

    >
    > YES. When i declare/use map without Compare everything works fine.
    > My compare:
    > struct Compare{
    > bool operator()(sha1 *sha1, sha1 *sha2) const{
    > char *s1;
    > char *s2;
    > s1=sha1_sprintf_hex(sha1);
    > s2=sha1_sprintf_hex(sha2);
    > if (strncmp(s1,s2,40)==0){
    > return false;
    > }
    > return true;
    > }
    > };
    >
    > Where sha1 object represents 160bit SHA1 value, sha1_sprintf_hex(sha)
    > returns pointer to char table (nice formatted SHA1 string).
    >
    > What could be wrong with that ?


    Because compare should test for less than not equality.

    struct Compare{
    bool operator()(sha1 *sha1, sha1 *sha2) const{
    char *s1;
    char *s2;
    s1=sha1_sprintf_hex(sha1);
    s2=sha1_sprintf_hex(sha2);
    return strncmp(s1,s2,40) < 0;
    }
    };

    john
     
    John Harrison, Sep 22, 2004
    #5
  6. "John Harrison" <> wrote in message
    news:...
    >
    > "vertigo" <> wrote in message
    > news:cirj6o$2h2$...
    > > > Maybe your Compare is broken? What does it do?

    > >
    > > YES. When i declare/use map without Compare everything works fine.
    > > My compare:
    > > struct Compare{
    > > bool operator()(sha1 *sha1, sha1 *sha2) const{
    > > char *s1;
    > > char *s2;
    > > s1=sha1_sprintf_hex(sha1);
    > > s2=sha1_sprintf_hex(sha2);
    > > if (strncmp(s1,s2,40)==0){
    > > return false;
    > > }
    > > return true;
    > > }
    > > };
    > >
    > > Where sha1 object represents 160bit SHA1 value, sha1_sprintf_hex(sha)
    > > returns pointer to char table (nice formatted SHA1 string).
    > >
    > > What could be wrong with that ?

    >
    > Because compare should test for less than not equality.
    >
    > struct Compare{
    > bool operator()(sha1 *sha1, sha1 *sha2) const{
    > char *s1;
    > char *s2;
    > s1=sha1_sprintf_hex(sha1);
    > s2=sha1_sprintf_hex(sha2);
    > return strncmp(s1,s2,40) < 0;
    > }
    > };
    >


    Also the way you are using sha1_sprintf_hex looks dubious. If you are
    returning a pointer to a dynamically allocated string then you have a memory
    leak, if you are returning a pointer to a staticly allocated array then you
    have a bug. Perhaps you should post the code for sha1_sprintf_hex.

    john
     
    John Harrison, Sep 22, 2004
    #6
  7. vertigo

    vertigo Guest

    When i always return with false:
    struct Compare{
    bool operator()(sha1 *sha1, sha1 *sha2) const{

    return false;
    }
    };

    i can add only one object to map (that OK).
    But when i always return with true, i can add for example 5 objects but
    iterator still iterate only fhtu two of them (like described earlier).
    ....
     
    vertigo, Sep 22, 2004
    #7
  8. vertigo

    vertigo Guest


    > Also the way you are using sha1_sprintf_hex looks dubious. If you are
    > returning a pointer to a dynamically allocated string then you have a memory
    > leak, if you are returning a pointer to a staticly allocated array then you
    > have a bug. Perhaps you should post the code for sha1_sprintf_hex.


    yes, that's other problem i must resolve.
    I edited Compare as you said and everything works fine:)

    Thanx
    Michal
     
    vertigo, Sep 22, 2004
    #8
  9. "vertigo" <> wrote in message
    news:cirjdf$oh1$...
    > When i always return with false:
    > struct Compare{
    > bool operator()(sha1 *sha1, sha1 *sha2) const{
    >
    > return false;
    > }
    > };
    >
    > i can add only one object to map (that OK).
    > But when i always return with true, i can add for example 5 objects but
    > iterator still iterate only fhtu two of them (like described earlier).
    > ...


    If you don't write you comparison functor in the correct way you get
    unpredictable results. It must return true if sha1 < sha2 and false
    otherwise.

    john
     
    John Harrison, Sep 22, 2004
    #9
  10. vertigo wrote:
    > When i always return with false:
    > struct Compare{
    > bool operator()(sha1 *sha1, sha1 *sha2) const{
    > return false;
    > }
    > };
    >
    > i can add only one object to map (that OK).
    > But when i always return with true, i can add for example 5 objects but
    > iterator still iterate only fhtu two of them (like described earlier).
    > ...


    The thing is that keys in the map are compared using only that Compare
    function: a == b if neither a < b nor b < a.
    So if Compare always return true, then a < b for any a and b; in
    particular a is never equal to a, and map can do whatever it wants to
    make you feel crazy.

    Yevgen
     
    Yevgen Muntyan, Sep 22, 2004
    #10
    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. Hendrik Maryns
    Replies:
    18
    Views:
    1,429
  2. greg
    Replies:
    6
    Views:
    459
    Dietmar Kuehl
    Jul 17, 2003
  3. wolverine
    Replies:
    3
    Views:
    1,078
    Chris
    Jul 31, 2006
  4. Vikram Karandikar
    Replies:
    1
    Views:
    199
    Vikram Karandikar
    Oct 25, 2013
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    132
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page