rand() question in Maps

Hello. I have a question in regards to filling a map with a random number. What I am trying to do is fill my map with zero's then fill the map again with random numbers. I coded the first output statement to check and see if they are all zeros and the next output to see if they had random numbers. I also wanted to change the index in the map from 0 to 1 after it was filled with the random numbers.
So, before the random number fill my map should be m<0, 0>
and after m<random #, 1>
My output is less than desirable. This is what I get:
0 0

0 0
26 1
Each time I run it the bottom numbers (26 1) change but the top displays the same
2 lines of zero's.

#include <iostream>
#include <map>
using namespace std;

int main ()
{

map<int, bool> m;

for(int a = 0; a <= 10; a++)
{
int g = 0;
bool b = 0;
m.insert(pair<int, bool>(g, b));
}

for (map<int, bool>::iterator position = m.begin(); position != m.end(); ++position)
{
cout << position->first << " " << position->second << endl;
cout << endl;
}


for(int i = 0; i <= 10; i++)
{
srand(time(NULL));
int num = rand()%52;
bool b = 1;
m.insert(pair<int, bool>(num, b));
}

for (map<int, bool>::iterator position = m.begin(); position != m.end(); ++position)
{
cout << position->first << " " << position->second << endl;
}

system ("pause");
return 0;
}

Any help would be great. Thanks
What are you trying to do? It looks to me more like you want vector<pair<int, bool> >. This isn't how maps work. http://www.cplusplus.com/map Also, you can't use pair unless you include <utility>
Last edited on
You're mapping the value false to the key 0... 11 times in a row, which doesn't make sense. So yeah, you probably were looking for a vector.
Also, you need to look up what srand does as well. You're supposed to call it once at the beginning of your program. As it is now, you'll almost always get the same "random" number 11 times in a row.

Edit: okay, two more:
you should use true and false instead of 1 and 0 for boolean constants.
And don't use system("pause"). Instead, use an IDE that keeps the console window open after the program terminates, such as Code::Blocks.
Last edited on
Or just use cin.get() on it's own line by itself.
Last edited on
I was trying to initialize the map to zero, both key and index and then trying to initialize it with 10 random numbers while turning the map index to 1.

Should I access the map using m[] instead of m.insert(pair<int, bool>(g, b))?
I don't know anything about vectors. I found the map insert function on line and was trying to understand it and make it work.


I will try moving the srand to the beginning of the program and start using cin.get() .
Thanks for your help.
You cannot store duplicate keys within a map. If you would like to store key, value pairs with duplicated keys then you should go for multimap container. However, for what you are doing, I do not think you need a multimap. I would suggest you to not initialize your map at all with 0 value for all key elements. Instead, I would just directly insert the key-value pairs into the map. I would either use the subscript operator or the insert method though the insert method is little more efficient than the subscript operator. When you want to access a map, to figure out if a particular key exists, you may use either the find or the count methods of the map container.
Maps are just what their name implies. They map one value (the key) to another. One distinct key can only have one value. See here:
http://www.cplusplus.com/reference/stl/map/

Should I access the map using m[] instead of m.insert(pair<int, bool>(g, b))?

Yes, you should. It won't change the behavior in any way - both do the same.
It's just shorter to write m[g]=b; (or rather: m[0]=0;) than m.insert(pair<int, bool>(g, b));

m[0]=0; also makes it more obvious that you're doing the same thing several times in a row and that this is pointless.

I was trying to initialize the map to zero

There's no need to "initialize" a map - you can only map values to other values. So in your "initialization" you're just mapping 0 to 0, which you probably didn't intend to do. Then again, it is not clear what you're trying to do in the first place.

I don't know anything about vectors.

Time to change that. vectors are more basic and more important than maps, so they come first (think of them as dynamic arrays).
Details are here:
http://www.cplusplus.com/reference/stl/vector/
Last edited on
Time to change that. vectors are more basic and more important than maps, so they come first (think of them as dynamic arrays).


Actually it depend on the situation. For most business-centric application development I have developed, I use map MORE often than vector. I believe this is strongly influenced in the way data is stored in those relational database. Since there is primary key to uniquely identify a record, when we model the relational data in our in-memory program we will naturally tend to use map, multi-map over vector.

As for C++ STL algorithm, it is a god-send gift. Very often we need customized sorting, searching etc in business applications and this fits in very well indeed. In this aspect I hold C++ STL algorithm classes on par with Java SDK Collections, Arrays class or even better :P
Topic archived. No new replies allowed.