473,413 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

std::map & compare

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
Sep 22 '05 #1
3 3237
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.
Sep 22 '05 #2
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?

Sep 22 '05 #3
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
Sep 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Kin Pang | last post by:
Hi, I have a routine where I'm using a std::map from a pair of ints to double for caching evaluation results. The cache depth could be upto say 1000 in depth. However, my application needs to...
5
by: cppaddict | last post by:
Hi, I'm confused about what the comparison operator in a map template is: In particular, I want to know why something like the following doesn't work: bool pointCompare(POINT p1, POINT p2)...
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
4
by: vineoff | last post by:
What kind of user-defined compare-function should be passed to std::map as a template argument? Like: std::map<foo, bar, cmp> map; Question 2: Is std::map resorted after every...
3
by: tezheng | last post by:
since std::map is some kind of std::set<std::pair> can i use "set_difference" with std::map? how to deal with the "operator =" of std::pair<T1, T2>, as T1 and T2 could be various type.
4
by: bb | last post by:
Hi, void fun(const std::map<std::string,int>& m1) { // How to make a case insensitive search of this map without making a copy? } cheers.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.