Hi, I have a map object with a key of integers.
std::map<int, CJob> m_PendingJobs;
I need to generate new unique key values. I could just find the largest key with a reverse iterator and add one. A remote, but conceivable problem is that the key values keep growing until they are too large for int. Since I ecpect elements of the map to be removed at a pretty good rate, I decided to find the first available key value. Here is an idea of how to do this.
Anyone have any better ideas?PHP Code:int NewKey = 0;
BOOL FoundKey = FALSE;
//the sequence of the map is ordered according to the key, integers, so last element has largest key value
//Use a reverse iterator to retrieve this key value. Search for the first available key, if none found use last
//key value plus one. If map is empty use 0.
if (!m_PendingJobs.empty())
{
std::map<int,CJob>::reverse_iterator rit = m_PendingJobs.rbegin();
for (int i = 0; i < (*rit).first; i++)
{
if (m_PendingJobs.find(i) == m_PendingJobs.end())
{
NewKey = i;
FoundKey = TRUE;
}
}
if (!FoundKey)
NewKey = (*rit).first + 1;
}
