Hi, i need some help, i would like to generate unique random alphanum strings for different products in my application, yesterday i asked this question but i got something close to a solution before anyone replied so i deleted the thread but after examining my solution thoroughly i found that yeah it could give me some random text, not very unique and totally irreproducible, this was not what i wanted
Is it possible to have a function maybe similar to a hashing function, that will take a class object and assign a random unique string to the object and that function should reproduce the same ID if the object is passed as an argument again
Note: i might have multiple copies of some objects F.E if Bar is an object
of the class Foo then at some instances i might have a container
containing 10 copies of Bar, i would also like to assign unique IDs even
to such objects that are copies of others.
to reproduce random strings you need to have the same seed.
You may have an static int which is a unique id. Each object that participate takes the id and increase it (note that's not thread safe). Then you can use the object id as a seed for random.
Note that setting the global seed influences all random numbers.
@coder777 that makes sense now have the same seed,staticint which is a unique id, ++ perfect , but say i would also like to have a search function in my application that will search for the object using, this unique string ID, i would like to pass a temporal class object to the function and retrieve the real object mapped to this ID F.E:
1 2 3 4 5 6 7 8
Foo temp;///temporaly object
Foo& search_with_ID(Foo temp)
{
auto obj_id=generate_random(temp);
return Foo_store[obj_id];///Foo_store is a map object
}
will that be possible, just trying to figure if i can get something like that working.
F.E(for example) :<> , leave alone the example above , i might not have explained it perfectly but basically the whole idea was to use the unique ID to ease the look up like we do using bar/Qr codes.
You could also use the object's address as a unique ID. This is easier but it won't work if the objects can be moved in memory, such as when writing them out to a file and reading them back in during a subsequent run of the program.
Say i would also like to have a search function in my application that will search for the object using, this unique string ID
Use an std::map<> to map string ID to object address.
Surely @dhyden i intend to use a map to store the objects but on the other hand my challenges are on the rises , am also sure that my objects will certainly be moved in memory from time to time so using the address as the ID presents yet another challenge
This are the reasons why i needed this ID
1. be able to differentiate between similar objects :- think of a store or a library
you might have 100 copies of the book "Effective STL" or whatever else that can be
stocked in there, how would you be able to differentiate between each of those copies.
2. Ease look up -
I might want to check if one of those copies is still in stock, i would not like to loop
100 or so times while i can access the item in O(1) using the ID.