Map stuff

I've been having some problems with maps.

First, how can I find the first free entry?

Second, how can I do a revese map, without doing something like this : map<int, class *>
map<class *, int>?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>>  first free entry
There are no "free" entries.   You can use begin() to get the first entry.

>> how can I do a revese map
There are lots of ways, but they will all involve a lot of programming and in the end will have absolutely no benefit over using a 2nd map.  So just use a 2nd map.

If you want to make this a little more automated, you can create your own "bi-directional" map class, that sues two maps as data members in its implimentation.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tomb64Author Commented:
the free entry part, I need to find the first entry that I can set a value to and it won't overwrite an others, thats what I need to do
maps don't work that way.  They don't have free entries.  They store exactly the entries you've added to them.  If you need to add another one, just add it.   You can use the [] operator to add an entry or use the insert() function.

A map is not like an array.

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tomb64Author Commented:
no my problem is that I need to find out if x can be set, without overwritting y

like this
mMap[1] = y;
mMap[3] = y;

I would need to find out that 2 is open
A map is not an array.  There is no entry for 2 in that map.  If you do

mMap[1] = y;
mMap[1000] = y;

there are 2 entries in the map, not 1000.  There are no unused entries, both entries are used.

You can use the find() member function to determine if a particular key is stored in a map, but the way you are discussing this strongly suggests that you intend to use the map incorrectly.  it is not meant to be used like an array.  The [] operator makes it seem like it is an array, but it isn't has an intirely different behavior.  It would be extremely inefficient to use it as an array.  The vector class is meant to be used like an array.
Tomb64Author Commented:
umm, ok then how would I do something like HWNDs in win32, thats all I'm trying to do I just need some creation code, because I can't have two plug-ins that have the same ID
I would store the ID's in a map as you are doing now.  This is because the map can be quickly searched for a particular ID (to retreive the data associated with that ID and to learn if that ID is in use.)  But the map should only store the ID's in use, not provide new ones.  You can use either use a static integer counter to provide the IDs or use a random number.  If you use the counter the IDs will all be in order, which is more organized, but it means that the IDs will be close to each other, which may cause confussion during debugging.  If you use a random number the ID's will be farther appart, but in random order.  For either choice when you obtain a new (potential) ID you must search the map using find() to make sure that the ID is not in use before you use that ID.   You certainly need this for the random number approach, but you also need for the counter approach because eventually the counter will "rollover" to 0 and allow you to reuse IDs.  Some of the reused IDs will still be in use.
Tomb64Author Commented:
Hows this, use Iters to loop trought, checking to see if the difference of the key is larger than 1?
Again you are asking a question that suggests that you are trying to use the map incorrectly.  If you feel the need to have a container where entries are sequential integers and you want to fill in each position if possible, then you don't want a map.  You want a vector.  A vector is like an array.   A map is not!  

You can do this with a map, but it is not efficient and it is not necessary.  It is "necessary" with a vector because if you have an entry with an index of 0 and an entry with an index of 1000, then you have 10001 entries stored.  With a map you could have just the two entries stored.  That means that there is no need to go to the trouble of trying to fill in "unused" entries in a map and doing so is costly.  (Trying to fill in "unused" entries in a vector is not nearly as costly and is wise since the "unused" entries take up space.)

Did you not understand the solution I suggested?  The map would be used to simply test to see if an ID is inuse, it would not be used to determine new IDs  (It is not really good for that purpose).  You would use a global counter or a random number generator to get a possible ID.  Before using that ID you would search the map (maps are good at this) to see if the ID is really free.

However, if you insist:

map<int,?>::iterator itr

itr = TheMap.first();
int LastNum = -1; // -1 is invalid

while (itr != TheMap.last())
   int Num = Itr->first;
   if (LastNum != -1)
      if (Num > LastNum + 1)
   LastNum = Num;

// LastNum + 1 is a skipped value.

Note that this is not really a good way to use a map.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.