std::map & compare

V

vertigo

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
 
R

red floyd

vertigo said:
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.
 
P

Pete C

vertigo said:
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?
 
M

Mark P

vertigo said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top