std::map & compare

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

  1. vertigo

    vertigo Guest

    Hello

    I have std::map object and i want to have randomly sorted objects in it.
    I tried to:

    std::map<int,RandomCompare> myobject;

    and:
    struct RandomCompare{
    bool operator(int i1, int i2){
    /* what shuold i put here ??? */
    }
    }

    I see that i can only return true or false, but shouldn't it return int to
    indicate 3 possibilities: equal, greater, smaller ?

    When i put there: return rand()%2; after adding several object's i have
    only some of them (because of true which symbols equality ??).

    How can i solve my problem ?
    I want to have all objects but while they are added always in the same
    order i want to read them (using iterator) in randomly order.

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

  2. vertigo

    red floyd Guest

    vertigo wrote:
    > Hello
    >
    > I have std::map object and i want to have randomly sorted objects in it.
    > I tried to:
    >
    > std::map<int,RandomCompare> myobject;
    >
    > and:
    > struct RandomCompare{
    > bool operator(int i1, int i2){
    > /* what shuold i put here ??? */
    > }
    > }
    >
    > I see that i can only return true or false, but shouldn't it return int
    > to indicate 3 possibilities: equal, greater, smaller ?
    >


    No, the compare function is a partial ordering. Return true if i1 is
    conceptually less than i2, return false if i1 is conceptually greater
    than or equal to i2.
     
    red floyd, Sep 22, 2005
    #2
    1. Advertising

  3. vertigo

    Pete C Guest

    vertigo wrote:
    > I have std::map object and i want to have randomly sorted objects in it.


    "Randomly sorted"? Is there such a thing?
    Anyway, a map needs to be sorted so as to do lookups on the key.
    Of course, you could always use a random integer as the key...
    Is there any reason you couldn't use a vector or deque, and use
    std::random_shuffle to randomise the order?
     
    Pete C, Sep 22, 2005
    #3
  4. vertigo

    Mark P Guest

    vertigo wrote:
    > Hello
    >
    > I have std::map object and i want to have randomly sorted objects in it.
    > I tried to:
    >
    > std::map<int,RandomCompare> myobject;
    >
    > and:
    > struct RandomCompare{
    > bool operator(int i1, int i2){
    > /* what shuold i put here ??? */
    > }
    > }
    >
    > I see that i can only return true or false, but shouldn't it return int
    > to indicate 3 possibilities: equal, greater, smaller ?
    >
    > When i put there: return rand()%2; after adding several object's i have
    > only some of them (because of true which symbols equality ??).
    >
    > How can i solve my problem ?
    > I want to have all objects but while they are added always in the same
    > order i want to read them (using iterator) in randomly order.
    >
    > Thanx
    > Michal


    Your approach (a comparator which returns a random result) will not work
    because a map cannot contain two objects which compare equal. For a
    map, a equals b if neither of the following is true: a < b, b < a.
    Since your comparison function is random, there's a good chance that any
    two objects will compare equal and the insert will not take place. Even
    worse, you'll get unpredictable (probably undefined) behavior since the
    comparison value changes arbitrarily and the map internally probably
    assumes its elements are in sorted order.

    Instead of (ab)using a map like this, why don't you use a vector and
    random_shuffle?

    Mark
     
    Mark P, Sep 22, 2005
    #4
    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. Peter Jansson
    Replies:
    5
    Views:
    6,424
    Ivan Vecerina
    Mar 17, 2005
  2. Replies:
    1
    Views:
    451
    red floyd
    Dec 21, 2008
  3. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,049
    James Kanze
    Dec 22, 2008
  4. James Kanze
    Replies:
    0
    Views:
    2,066
    James Kanze
    Dec 21, 2008
  5. nw
    Replies:
    5
    Views:
    405
    Permostat
    Mar 12, 2010
Loading...

Share This Page