473,404 Members | 2,187 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,404 software developers and data experts.

Find unused ID.

I've map<unsigned int, Class*>. I'd like to find unused ID.
Is it easier (and/or faster) code:
template<class Class>
int findID(map<unsiged int, Class*>)
{
unsigned int max;
set<unsigned int> Set;
for(map<unsigned int, Class*>::iterator it = mymap.begin(); it !=
mymap.end; it++)
{
Set.insert(it->first);
if(it->first > max)
max = it->first;
}
for(unsigned int i = 0; i <= max; i++)
if(Set.find(i) == Set.end())
return i;
return max + 1;
}
Regards.
--
Linux user: #376500 (patrz http://counter.li.org/)
Jul 23 '05 #1
1 1781
Uzytkownik a écrit :
I've map<unsigned int, Class*>. I'd like to find unused ID.
Is it easier (and/or faster) code:
template<class Class>
int findID(map<unsiged int, Class*>)
{
unsigned int max;
set<unsigned int> Set;
for(map<unsigned int, Class*>::iterator it = mymap.begin(); it !=
mymap.end; it++)
{
Set.insert(it->first);
if(it->first > max)
max = it->first;
}
for(unsigned int i = 0; i <= max; i++)
if(Set.find(i) == Set.end())
return i;
return max + 1;
}
Regards.


Just look for it directly in the map:
for ( unsigned int i=0;
i < std::numeric_limits<unsigned int>::max();
i++)
if (mymap.find(i) != mymap.end())
return i;

It may not be the fastest code, but it is the easiest.

The fastest should be:
unsigned int i = 0;
for ( map<unsigned int, Class*>::iterator it = mymap.begin();
it != mymap.end();
it++, i++))
if (it->first != i)
return i;

Indeed, inside std::map, the data is sorted by its key member, so you
just have to check non corresponding ones.

--
Jul 23 '05 #2

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

Similar topics

5
by: Brad Tobin | last post by:
On a production database, there is a 2GB database, when I run sp_spaceused it indicates a very high quanity of unused space. The database has been shrunk & free space sent to the OS. Why is this...
15
by: Eirik | last post by:
This is a little function I wrote, inspired by the thread "Urgent HELP! required for Caesar Cipher PLEASE" $ cat /home/keisar/bin/c/ymse/rot13.h char rot13(char character) { int changed;...
12
by: zacks | last post by:
Suddenly, in a VB2005 project I am working on, several variables show up in the list of warnings are being unused local variables. But they all are. Several of them are the ex variable used in a...
29
by: Ajay | last post by:
Hi all,Could anybody tell me the most efficient method to find a substr in a string.
8
by: Frank Rizzo | last post by:
Is there a setting in VS2005 to quickly locate methods that are unused (maybe through compiler warnings)? If not, any utilities out there that do that? Thanks
33
by: jacob navia | last post by:
Hi I am considering extending the lcc-win32 compiler to accept int fn(int a, int b,double) { // body of the function } This allows the programmer to specify that the third parameter is not...
6
by: nickvans | last post by:
Hi all, I have a table called tblRecords that has "DashNum" as its primary key. The lowest value of this table is 116 and the highest value is 269, though there are some missing values. It is...
13
by: Rex Mottram | last post by:
I'm using an API which does a lot of callbacks. In classic callback style, each routine provides a void * pointer to carry user-defined data. Sometimes, however, the user-defined pointer is not...
1
colinod
by: colinod | last post by:
I have the following to determine unused parts of a database and was wondering how to find it if the cell is just empty instead of having the word unused in it <% if...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.