Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::map & compare

Reply
Thread Tools

std::map & compare

 
 
vertigo
Guest
Posts: n/a
 
      09-22-2005
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      09-22-2005
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.
 
Reply With Quote
 
 
 
 
Pete C
Guest
Posts: n/a
 
      09-22-2005
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?

 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      09-22-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare 2 arrays consisting of hashes Frodo Larik Perl 1 05-30-2004 05:02 PM
How to compare strings Thomas Reinemann VHDL 0 05-27-2004 02:24 PM
compare unsigned Matthias Alles VHDL 1 05-14-2004 03:14 PM
Compare pairs of bits between two slv's ? Tony Benham VHDL 3 11-02-2003 12:29 PM
Perl Newbie: compare two files contents with same pattern YC Perl 1 08-13-2003 10:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57