tags:

views:

97

answers:

2

I want to generate any limited std::string size unique id (i.e of size 6) in 32 bit application. what would be the best and quick way to do this?

+6  A: 

Look up hashing of strings, e.g. the Jenkins hash function.

But you will never get unique hashes, because strings can be much longer than your size 6, and the Pigoenhole lemma shows trivially that hashes must collide as a a consequence.

Konrad Rudolph
+2  A: 

Very difficult to tell from your question what you are asking, but the following generates strings in the sequence "1", "2", "3":

#include <string>
#include <sstream>

std::string GetUniqueId() {
   static int n = 1;
   std::ostringstream os;
   os << n++;
   return os.str();
}
anon
I'm saving the data with unique IDs in a file, If i were to use 1.. 2.. 3.. sequence then how would i know if the ID was already given to some data the next time i load the file?I guess it would be better if I generate the id's from each unique data that may have ID's.
Dave18
Why didn't you state that in your original question? The quality of the answers you get here is directly proportional to the clarity and detail of your question.
anon